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 D59C0C48286 for ; Fri, 2 Feb 2024 02:28:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E7586B0072; Thu, 1 Feb 2024 21:28:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4960E6B0074; Thu, 1 Feb 2024 21:28:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 35DAB6B0075; Thu, 1 Feb 2024 21:28:12 -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 26C526B0072 for ; Thu, 1 Feb 2024 21:28:12 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C2E03406AD for ; Fri, 2 Feb 2024 02:28:11 +0000 (UTC) X-FDA: 81745279182.16.10F62DF Received: from mail-ot1-f51.google.com (mail-ot1-f51.google.com [209.85.210.51]) by imf15.hostedemail.com (Postfix) with ESMTP id CCF51A0011 for ; Fri, 2 Feb 2024 02:28:09 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=z09m+Cmq; dmarc=none; spf=pass (imf15.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.210.51 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=1706840889; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=nH6kvi7IzHM1HjT9d3ebo606kaCmtup5eOU1YOyCeeQ=; b=A+cMNF5gN844/2hz8MggRfgt/j3PDIju0+D8ge4CO/Lrgj07C2go/OPm3Dieqh0Jj0TvY8 ovP+Ve81RpJcmjGJDtMY5W7uRPOt8FbrE5h6G0vjo62NTllDOBg/lYK9QrKAPhNKEebrE/ RRoP1BUyFyTSCeT6xYSwTNR+SDByu3w= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=z09m+Cmq; dmarc=none; spf=pass (imf15.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.210.51 as permitted sender) smtp.mailfrom=charlie@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706840889; a=rsa-sha256; cv=none; b=dab6g9boSdVrDmXa6Ni987oAlEVo8btEouWgUpry7sXTchZV+QUM0vTmbn46ybiG3vw/NR BLuU6+imSSyGBAmEn5TlgW8LFwqqxw21FhMaNCko11bSSXZmW3iXJj48nMYrvM9M6bgGLU Oj57r69qlxtkNilAj2F5j25QqzE1r5Y= Received: by mail-ot1-f51.google.com with SMTP id 46e09a7af769-6e11cda9f6fso940943a34.2 for ; Thu, 01 Feb 2024 18:28:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706840889; x=1707445689; darn=kvack.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=nH6kvi7IzHM1HjT9d3ebo606kaCmtup5eOU1YOyCeeQ=; b=z09m+CmqDm8J0k41G+HTavymeG+TNuJSojJc6eiRAOBIFEDfNHqLg6vZESAhyLpxMf KfscBREF7x0Au+Da5aM78rkE9PbzKii5M1RlYp5zCFyZaKmJwKIYalq8NSkL5zYnEKRc BmACasrxjcY7YTiQARNfMBP++4qsYC7LCpkeVyFvXo+eAJcnxG5J3FT3AAiou8dLtxTN A+7sxHM4VumT7vvq94TfHoijmBF2k3QxENCNGiNy1EbrZnrTvb+PtUNl6e+3cl3QguID 18NGAYkxnVjNg26fTSCOqdvbF/bN8pkSjKKQg5NfS5N0vgYh+nHW64VpNwmdncLyLuo5 nKew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706840889; x=1707445689; h=in-reply-to:content-transfer-encoding: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=nH6kvi7IzHM1HjT9d3ebo606kaCmtup5eOU1YOyCeeQ=; b=aVy3vHMjgoEr0dSZTbZNy+4y75wYgzgVlAPsovhvhoh6jG7ED1LFOJG1bVIanQcwgH oVGG4VxOBgXDl+4g0vfXOJcRz6hBWcobhI8JTVrm18lL1fKhWqdbVNvVZvOR8OX3djLF muopFQ4ODnmtd0AdwBuJARtPIGFUvcMy08sXqDpEvdTvbEEHBXOv8/Ye9tGhn8Ixo6Ji BSEPxLS5BFQLthnfxFnr0cikoXjbhMcQV+P0nqNiPo9qQT1Sk/r99cv6OkEIbtUWmbpT h5fUVEUr3lXqTZMnMjrLPuqceK+IA4agmNKU7WE391R0Yrk07AiJsfZyUgobCsxfzFMb 7zJw== X-Gm-Message-State: AOJu0YzljUNLGMGGSmtOcUGKfgOQNKDgf3ORqYD2zQz48kxqQXs3Rvyr PlOZOY5k+r2AF+x1FBYppmZjko63ij0bF3IsZLhBPLF6j9eRrEE7rwxFS70ekrc= X-Google-Smtp-Source: AGHT+IFp062ooy/IHYTf+Z1OQbUuwLGyLVscZ2fxBHLPNsW3kl1YytnGZuKwPGTv6LdCdXItOvcJDA== X-Received: by 2002:a05:6358:6f9e:b0:175:f9b8:89bf with SMTP id s30-20020a0563586f9e00b00175f9b889bfmr754264rwn.22.1706840888844; Thu, 01 Feb 2024 18:28:08 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXzW8HfzfBFndPbvvprCc2R3w70/c3RF57w2kO5sNhkjXiUc1+iJh25RmbGSpHdo5iPTXeHAnAB7skw2x1KEoPntxLdqUFcfINGL9DWhTDZaEPfW02nxrhVVq93LOqGqNgq0r2qw1V7QZ4yQQb9qzK9vG4gCQm+HEhoLTBwLMa67+YJOLZ31OGsXEicpDP0DEqDcubfynAm/g8XPvNWtIf9b3BlmflK4EEeYcg2uN/vMC9b1V1V3JenxQ0f8Fdb7boDEqhfd/fOEWa8BjnidQ1VNbmJzh8ygiJqy/NzNF8GnnxzaQLPE/xjCNrGBSeDM8QyHJD2o/RIpcqCU7zdkZSx+UdVKZjT1sw6QR6wH8bkgpWrrk+HXWDd54eS+Qerhkl4EURj Received: from ghost ([12.44.203.122]) by smtp.gmail.com with ESMTPSA id f8-20020a63de08000000b005d8aef12380sm524464pgg.73.2024.02.01.18.28.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 18:28:08 -0800 (PST) Date: Thu, 1 Feb 2024 18:28:06 -0800 From: Charlie Jenkins To: Yangyu Chen Cc: Alexandre Ghiti , Paul Walmsley , Palmer Dabbelt , Albert Ou , Shuah Khan , Jonathan Corbet , linux-mm@kvack.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org Subject: Re: [PATCH v3 1/3] riscv: mm: Use hint address in mmap if available Message-ID: References: <20240130-use_mmap_hint_address-v3-0-8a655cfa8bcb@rivosinc.com> <20240130-use_mmap_hint_address-v3-1-8a655cfa8bcb@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: CCF51A0011 X-Stat-Signature: e5tpw1ceqod6panwm3dck3sxz876ug7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Q5k6Fxh2b4kiiMj2VfX59jDH7RzNLPAGUEHp98WBLhCX3CivjSc91/8Iv45eEWQ12m8f/OzkF18HHDTvUaNxUfmFlYtVl7S7kiRjAprdupfWeysZfDOmGNNtUcO5hWmXXbod5dvTQjVPOGXsjMA6e9BDN3jaiGGfhYFncWpX2ihnyraF48YR5zRzBd08dOArtIRGwmkwpahgYX15+WysvE7jy+ja87v/+244yx2CYT1HxsOJqtbueeDLs9QJb33azNfkxIcMN4mhzJZZNejGuqHo21220FFpBTefjFiTEhlvh4t4bB3M5QVHi8lf079SbCQYfou0mGFPYfJZPAiqZ9DzN5m6AqibPm9PfnZuJeHfHglgV3KwzEye4Wr80KCyPxJROYdBLUFpDEu85is6e7gkjZPBY+CmH7LXmEqgPvGLG7V8/bsC/PHlZEz1U= 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: On Wed, Jan 31, 2024 at 11:59:43PM +0800, Yangyu Chen wrote: > On Wed, 2024-01-31 at 22:41 +0800, Yangyu Chen wrote: > > On Tue, 2024-01-30 at 17:07 -0800, Charlie Jenkins wrote: > > > On riscv it is guaranteed that the address returned by mmap is less > > > than > > > the hint address. Allow mmap to return an address all the way up to > > > addr, if provided, rather than just up to the lower address space. > > > > > > This provides a performance benefit as well, allowing mmap to exit > > > after > > > checking that the address is in range rather than searching for a > > > valid > > > address. > > > > > > It is possible to provide an address that uses at most the same > > > number > > > of bits, however it is significantly more computationally expensive > > > to > > > provide that number rather than setting the max to be the hint > > > address. > > > There is the instruction clz/clzw in Zbb that returns the highest > > > set > > > bit > > > which could be used to performantly implement this, but it would > > > still > > > be slower than the current implementation. At worst case, half of > > > the > > > address would not be able to be allocated when a hint address is > > > provided. > > > > > > Signed-off-by: Charlie Jenkins > > > --- > > >  arch/riscv/include/asm/processor.h | 27 +++++++++++--------------- > > > - > > >  1 file changed, 11 insertions(+), 16 deletions(-) > > > > > > diff --git a/arch/riscv/include/asm/processor.h > > > b/arch/riscv/include/asm/processor.h > > > index f19f861cda54..8ece7a8f0e18 100644 > > > --- a/arch/riscv/include/asm/processor.h > > > +++ b/arch/riscv/include/asm/processor.h > > > @@ -14,22 +14,16 @@ > > >   > > >  #include > > >   > > > -#ifdef CONFIG_64BIT > > > -#define DEFAULT_MAP_WINDOW (UL(1) << (MMAP_VA_BITS - 1)) > > > -#define STACK_TOP_MAX TASK_SIZE_64 > > > - > > >  #define arch_get_mmap_end(addr, len, flags) \ > > >  ({ \ > > >   unsigned long > > > mmap_end; \ > > >   typeof(addr) _addr = (addr); \ > > > - if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && > > > is_compat_task())) \ > > > + if ((_addr) == 0 || \ > > > +     (IS_ENABLED(CONFIG_COMPAT) && is_compat_task()) || \ > > > +     ((_addr + len) > BIT(VA_BITS - > > > 1))) \ > > >   mmap_end = STACK_TOP_MAX; \ > > > - else if ((_addr) >= VA_USER_SV57) \ > > > - mmap_end = STACK_TOP_MAX; \ > > > - else if ((((_addr) >= VA_USER_SV48)) && (VA_BITS >= > > > VA_BITS_SV48)) \ > > > - mmap_end = VA_USER_SV48; \ > > >   else \ > > > - mmap_end = VA_USER_SV39; \ > > > + mmap_end = (_addr + len); \ > > >   mmap_end; \ > > >  }) > > >   > > > @@ -39,17 +33,18 @@ > > >   typeof(addr) _addr = (addr); \ > > >   typeof(base) _base = (base); \ > > >   unsigned long rnd_gap = DEFAULT_MAP_WINDOW - (_base); \ > > > - if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && > > > is_compat_task())) \ > > > + if ((_addr) == 0 || \ > > > +     (IS_ENABLED(CONFIG_COMPAT) && is_compat_task()) || \ > > > +     ((_addr + len) > BIT(VA_BITS - > > > 1))) \ > > >   mmap_base = (_base); \ > > > - else if (((_addr) >= VA_USER_SV57) && (VA_BITS >= > > > VA_BITS_SV57)) \ > > > - mmap_base = VA_USER_SV57 - rnd_gap; \ > > > - else if ((((_addr) >= VA_USER_SV48)) && (VA_BITS >= > > > VA_BITS_SV48)) \ > > > - mmap_base = VA_USER_SV48 - rnd_gap; \ > > >   else \ > > > - mmap_base = VA_USER_SV39 - rnd_gap; \ > > > + mmap_base = (_addr + len) - rnd_gap; \ > > >   mmap_base; \ > > >  }) > > >   > > > +#ifdef CONFIG_64BIT > > > +#define DEFAULT_MAP_WINDOW (UL(1) << (MMAP_VA_BITS - 1)) > > > +#define STACK_TOP_MAX TASK_SIZE_64 > > >  #else > > >  #define DEFAULT_MAP_WINDOW TASK_SIZE > > >  #define STACK_TOP_MAX TASK_SIZE > > > > > > > I have carefully tested your patch on qemu with sv57. A bug that > > needs > > to be solved is that mmap with the same hint address without > > MAP_FIXED > > set will fail the second time. > > > > Userspace code to reproduce the bug: > > > > #include > > #include > > #include > > > > void test(char *addr) { > >     char *res = mmap(addr, 4096, PROT_READ | PROT_WRITE, > > MAP_ANONYMOUS > > > MAP_PRIVATE, -1, 0); > >     printf("hint %p got %p.\n", addr, res); > > } > > > > int main (void) { > >     test(1<<30); > >     test(1<<30); > >     test(1<<30); > >     return 0; > > } > > > > output: > > > > hint 0x40000000 got 0x40000000. > > hint 0x40000000 got 0xffffffffffffffff. > > hint 0x40000000 got 0xffffffffffffffff. > > > > output on x86: > > > > hint 0x40000000 got 0x40000000. > > hint 0x40000000 got 0x7f9171363000. > > hint 0x40000000 got 0x7f9171362000. > > > > It may need to implement a special arch_get_unmapped_area and > > arch_get_unmapped_area_topdown function. > > > > This is because hint address < rnd_gap. I have tried to let mmap_base = > min((_addr + len), (base) + TASK_SIZE - DEFAULT_MAP_WINDOW). However it > does not work for bottom-up while ulimit -s is unlimited. You said this > behavior is expected from patch v2 review. However it brings a new > regression even on sv39 systems. > > I still don't know the reason why use addr+len as the upper-bound. I > think solution like x86/arm64/powerpc provide two address space switch > based on whether hint address above the default map window is enough. > Yep this is expected. It is up to the maintainers to decide. - Charlie