From mboxrd@z Thu Jan 1 00:00:00 1970 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 F0334C47258 for ; Sat, 20 Jan 2024 06:49:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 692596B0075; Sat, 20 Jan 2024 01:49:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 61AEC6B0078; Sat, 20 Jan 2024 01:49:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4BC0E6B007B; Sat, 20 Jan 2024 01:49:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 36F4A6B0075 for ; Sat, 20 Jan 2024 01:49:41 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C72BEC03AA for ; Sat, 20 Jan 2024 06:49:40 +0000 (UTC) X-FDA: 81698763720.28.13FAAE1 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by imf18.hostedemail.com (Postfix) with ESMTP id DEDF61C0002 for ; Sat, 20 Jan 2024 06:49:38 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Awty+HQo; dmarc=none; spf=pass (imf18.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.216.49 as permitted sender) smtp.mailfrom=charlie@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705733379; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=tMNq17q3D6EGfQkVBOfkiQr/f5qkK8wzXeUaA36zW64=; b=oN06AcHZbF3oqWuRjrCyl1rHRQLnxNOhw1AglGjCUOWXWeLIXo1P39AwGEJkgPCYtSYjcP fkRa9ykvxDr/BlScLkf3X57NQYhv8nW9KoOCc13rpI+t0X/2GDzD5iPHHsperi2ati5d4V RZ0ouI7uFMMdB+lLCwcuC/C2Mdaasrw= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Awty+HQo; dmarc=none; spf=pass (imf18.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.216.49 as permitted sender) smtp.mailfrom=charlie@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705733379; a=rsa-sha256; cv=none; b=zdTH7cdFQEdE5O8NdyVNBCPPI1Qo/Mf3szNHUx/0/rGvftwwo+tSUU7U54IICQAnnOnRv3 5Z644T97S6Vb/fsxLgybi1loxTa7NcPWbPSxdgPwX77palLUZKNMvBOl2P/OSZ4+zbWT0i XXJN07qq+cO23cMQeRB1lxx6au4ymOk= Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-2901f9a9d00so1275178a91.1 for ; Fri, 19 Jan 2024 22:49:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1705733377; x=1706338177; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=tMNq17q3D6EGfQkVBOfkiQr/f5qkK8wzXeUaA36zW64=; b=Awty+HQonyK7aEkNJwMH/fusAd29W7rrRxLXbLl4bUMR86ZQB5IcjBPa8qbB6hIeqC P59Ddq4TwObA3Ybcxaveqstefy4H0VkhRu4vMw92ryMB/ZvrO91b4JS2nks3FiUOzOkc 6Q6Z+dWLqfGe7pAZYdgBLyg87CpL4iYlnicSNjTWNdJn9S7RlYOm4h2BOGwWXydbtEgu psBryWDWDVX48tomYt0rasvuxvzX58nR0YQd/iBbAJTXYsT8lqfhRIRxzrYrkRApN8B6 ZN7S/KcYHlM/CE7EZltsp5nnhQGckMndbY2rVGgyQYFIFMcg49nxhbeFH4j3EwIQieQA 4tUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705733377; x=1706338177; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=tMNq17q3D6EGfQkVBOfkiQr/f5qkK8wzXeUaA36zW64=; b=vWRVntUHNps1KAfmOok0LrR3pMIO5RWSLb7io4DXy8Gax1g3Sivn6Dh4aAy6NdVCHS 4UxyAFC78RNrryEeIq7SMwIoB+oun/+enayGVKqD/DBFZu73S2xwK7612c9cDDBDvMja qpBhSsPyTM+pFLJZZVryEvUrWpjgxdK9PxUGyKRU9eruQRtU71gtWHbAj+WihWVO+3/H jlMK7/fcR5wgPuthDWlBshf1SiD3CEIV+P3V8DfZl4lVCJMQG5K+qY+/cP/QYgw9ii72 Bt1kzMl18Q9e2jxeRec6mz7tFSkNTm7DYGOR149XHxTR0uROyhEilrbT0CzktNOM7qok ZfDA== X-Gm-Message-State: AOJu0YyV1iwpkGFAXd2DQaiMFPJE+aq4V4QtgkLAtborMXTu1vbG4sRQ /2OqI61sc6Qra4qqRcHxfzH1V81aAR0SUjBWDB+myTx4rl7dwPTDvbLGWrGGhac= X-Google-Smtp-Source: AGHT+IHuiDuKFM2UR73CaJ02HXo9ean1HBHrvYu61hM8hW45PnG18N9ghGARYqYc2iZy9dYx7vea7Q== X-Received: by 2002:a17:903:2304:b0:1d5:231b:6ea3 with SMTP id d4-20020a170903230400b001d5231b6ea3mr1323587plh.16.1705733377493; Fri, 19 Jan 2024 22:49:37 -0800 (PST) Received: from ghost ([2601:647:5700:6860:9c37:413:ae5:27a4]) by smtp.gmail.com with ESMTPSA id y11-20020a170903010b00b001d706bb2711sm3790425plc.22.2024.01.19.22.49.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 22:49:36 -0800 (PST) Date: Fri, 19 Jan 2024 22:49:33 -0800 From: Charlie Jenkins To: Yangyu Chen Cc: alexghiti@rivosinc.com, anup@brainfault.org, aou@eecs.berkeley.edu, conor@kernel.org, jrtc27@jrtc27.com, konstantin@linuxfoundation.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, mick@ics.forth.gr, palmer@rivosinc.com, paul.walmsley@sifive.com, rdunlap@infradead.org Subject: Re: [PATCH v10 0/4] RISC-V: mm: Make SV48 the default address space Message-ID: References: <20230809232218.849726-1-charlie@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspam-User: X-Stat-Signature: rt96f9ey8s4en83xwhkf64fbpuzcebcr X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: DEDF61C0002 X-HE-Tag: 1705733378-729777 X-HE-Meta: U2FsdGVkX19kIJf8gC6CH5anL+0cEqri6W0nhOJW+DS/DqCvI/cbPs9Qsz/kYosd94YNfXzDpDyerjhXzFc0Yg6swrD8/je4oh4pxvRxmTcwlOjsqCtQxTaE99tKyPXpIImlY619VEgXsDsSTzgAPJTlzAc4xTXJe3UbRFpHhfDXi2h61aEwfzjeBPgT+p9ni0Tu+wXNdxRWHqqktK/SvBPfzPoCQVTMbk5bjKgckyRZSSD+dORhKIt+MJ30RACYc/h0Ys3LDefE+5FwJfmNCb9qm4FHoJVi/JeaZno0mgJ0gdlL9cmHKaNCjHTGxZR+e7Qixpt3H51+80BCFTALjw/gmMlZzq5muU4d5ROvYFzNrWO65+Bn4G5BjO8iAmGqv/H7YrVgR0d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Rurra0hpxeLonwDt5ziz5DZEeHLrG9/eQmdK60 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000005, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Sat, Jan 20, 2024 at 02:13:14PM +0800, Yangyu Chen wrote: > Thanks for your reply. > > On 1/20/24 09:34, Charlie Jenkins wrote: > > On Sun, Jan 14, 2024 at 01:26:57AM +0800, Yangyu Chen wrote: > > > Hi, Charlie > > > > > > Although this patchset has been merged I still have some questions about > > > this patchset. Because it breaks regular mmap if address >= 38 bits on > > > sv48 / sv57 capable systems like qemu. For example, If a userspace program > > > wants to mmap an anonymous page to addr=(1<<45) on an sv48 capable system, > > > it will fail and kernel will mmaped to another sv39 address since it does > > > > Thank you for raising this concern. To make sure I am understanding > > correctly, you are passing a hint address of (1<<45) and expecting mmap > > to return 1<<45 and if it returns a different address you are describing > > mmap as failing? If you want an address that is in the sv48 space you > > can pass in an address that is greater than 1<<47. > > > > > not meet the requirement to use sv48 as you wrote: > > > > > > > else if ((((_addr) >= VA_USER_SV48)) && (VA_BITS >= VA_BITS_SV48)) \ > > > > mmap_end = VA_USER_SV48; \ > > > > else \ > > > > mmap_end = VA_USER_SV39; \ > > > > > > Then, How can a userspace program create a mmap with a hint if the address > > > > = (1<<38) after your patch without MAP_FIXED? The only way to do this is > > > to pass a hint >= (1<<47) on mmap syscall then kernel will return a random > > > address in sv48 address space but the hint address gets lost. I think this > > > > In order to force mmap to return the address provided you must use > > MAP_FIXED. Otherwise, the address is a "hint" and has no guarantees. The > > hint address on riscv is used to mean "don't give me an address that > > uses more bits than this". This behavior is not unique to riscv, arm64 > > and powerpc use a similar scheme. In arch/arm64/include/asm/processor.h > > there is the following code: > > > > #define arch_get_mmap_base(addr, base) ((addr > DEFAULT_MAP_WINDOW) ? \ > > base + TASK_SIZE - DEFAULT_MAP_WINDOW :\ > > base) > > > > arm64/powerpc are only concerned with a single boundary so the code is simpler. > > > > As you say, this code in arm64/powerpc will not meet the issue I address. > For example, If the addr here is (1<<50) on arm64, the arch_get_mmap_base > will return base+TASK_SIZE-DEFAULT_MAP_WINDOW which is (1< And this behavior on arm64/powerpc/x86 does not break anything since we will > use a larger address space if the hint address is specified on the address > > DEFAULT_MAP_WINDOW. The corresponding behavior on RISC-V should be if the > hint address > BIT(47) then use Sv57 address space and use Sv48 when the > hint address > BIT(38) if we want Sv39 by default. > > However, your patch needs the address >= BIT(47) rather than BIT(38) to use > Sv48 and address >= BIT(56) to use Sv57, thus breaking existing userspace > software to create mapping on the hint address without MAP_FIXED set. Code that needs mmap to provide a specific address must use MAP_FIXED. On riscv, it was decided that the address returned from mmap cannot be greater than the hint address. This is currently implemented by using the largest address space that can fit into the hint address. It may be possible that this range can be extended to use all of the addresses that are less than or equal to the hint address. >From reading the code even on arm64 if you pass an address that is greater than DEFAULT_MAP_WINDOW it is not guaranteed that mmap will return an address that is greater than DEFAULT_MAP_WINDOW. It may still be provide an address that is less than DEFAULT_MAP_WINDOW if it fails to find an address above. This seems like this would also break your use case. > > > > violate the principle of mmap syscall as kernel should take the hint and > > > attempt to create the mapping there. > > > > Although the man page for mmap does say "on Linux, the kernel will pick > > a nearby page boundary" it is still a hint address so there is no strict > > requirement (and the precedent has already been set by arm64/powerpc). > > > > Yeah. There is no strict requirement. But currently x86/arm64/powerpc works > in this situation well. The hint address on these ISAs is not used as the > upper bound to allocating the address. However, on RISC-V, you treat this as > the upper bound. > > > > > > > I don't think patching in this way is right. However, if we only revert > > > this patch, some programs relying on mmap to return address with effective > > > bits <= 48 will still be an issue and it might expand to other ISAs if > > > they implement larger virtual address space like RISC-V sv57. A better way > > > to solve this might be adding a MAP_48BIT flag to mmap like MAP_32BIT has > > > been introduced for decades. > > > > > > Thanks, > > > Yangyu Chen > > > > > > > - Charlie > > >