From patchwork Wed Aug 28 05:49:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 13780626 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 25BAAC5474F for ; Wed, 28 Aug 2024 05:49:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACEFE6B009D; Wed, 28 Aug 2024 01:49:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A7FE96B009E; Wed, 28 Aug 2024 01:49:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 883896B009F; Wed, 28 Aug 2024 01:49:57 -0400 (EDT) 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 6954D6B009D for ; Wed, 28 Aug 2024 01:49:57 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 229121A1D3D for ; Wed, 28 Aug 2024 05:49:57 +0000 (UTC) X-FDA: 82500578034.14.2FFFB0D Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf10.hostedemail.com (Postfix) with ESMTP id 3166FC0015 for ; Wed, 28 Aug 2024 05:49:54 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=zCp3ItAz; dmarc=none; spf=pass (imf10.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.214.175 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=1724824097; 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=WPJ0dF9cF6J1lv0FlFvR4vKxUsSQiAia4h2dvI+DMjw=; b=TqXdoLO3yxfIjLpRdOblWTGBkHJUGKXljCTB0mo016nsxa2LI1JddFuzjUibygA3v9W70/ 5S9qbzyxuCJ4AjiY6ImYag2dbZ6500gc+KEgjDa7bNhen1SUFNuEVaW20YIPSkiwOt0T3H X3vcTjYg1vNZ7NLBFG1WMqZOnD85F08= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724824097; a=rsa-sha256; cv=none; b=4oqXsidjASHhoHgfvVei6U8D73TWKLS2XLIU+RQgnpbuH0E3WNyvNc7eZtrec692dFPAyM SE7R+IAUkMr7UqXgjHxhFjgR6jADvA7j9Hi5EGjMQKC0LOn38Kx4eDkctLQObPe5F3aUIs 9MQ0xERe0YlOKpOcZRYdaadVfW52cnk= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=zCp3ItAz; dmarc=none; spf=pass (imf10.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=charlie@rivosinc.com Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-20223b5c1c0so57718915ad.2 for ; Tue, 27 Aug 2024 22:49:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1724824194; x=1725428994; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=WPJ0dF9cF6J1lv0FlFvR4vKxUsSQiAia4h2dvI+DMjw=; b=zCp3ItAzVWgH6YUlikxo5CJvFQms+kn25YXdtfPS8pm0MjIoofadzlWOcuDPzAg8AK KJGNMrA7+KyIXTXRB+Bi1uAyJ1jdqGdHlXDiTMzB0UQLZe8PIOftwWFQ9s3LCsc9gdne L3NzuFxacGgF0fZ9yb6GvFPqK4lHJGKR1zAl/eTO/gbGW/3zJ96oiZHXdcJKm+0JH1SY cUMRmDRdTxGmU90RELlLAgd3/Vbk8gac4zD0aL593jsCjQEVgX+9Zd1Sk5GwTphqYQfN J9kGJcJi/ba/NZ8AvCTZkpYs1JD4Rlm3cKpdF4sizzbkfEsNnFkPoI828808GhR5bTEL Hkzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724824194; x=1725428994; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WPJ0dF9cF6J1lv0FlFvR4vKxUsSQiAia4h2dvI+DMjw=; b=OmMHA8oQmE+HNqA7s2GYmXVERFDppKi+MIjmzwKSO8v/f3R3LOQ0KmkhaC/kZvtjyX 1ylmxrQ/0jibyphHrdQwnmliGl8TQDq82nzdQfNb1pcUkDfur07uVWopxFcroFoUSVcW xm2G228CO3xSyGBNgdXJcALvPre+wwUAmZVY1wR2wdACSkAz/Sys939fSLI+mJjTvi5t jWW5nEey7y4bGolrHaGo5E/hD+GL4q3Ve0FYL8BKCCNdGTqth+tjnlClMNb4s/oxgXaI do8WaEg/f01Qkcsxlj0orYdm70RAYaWGxpq5fDT+bJF3lAVzCC/t1WUX2h677uaXIUGp X97A== X-Forwarded-Encrypted: i=1; AJvYcCXcCFZ3vGSbF91XJrhFPKt8+R71TW5gelq3b7Xv0c/6lHoDGJJx8LJuAqHKKsrwK84eCxDIO0BGBg==@kvack.org X-Gm-Message-State: AOJu0Yy7DsLPk8FSXA/F31x24Qum86/wxSM8GYOA6h6W3GRyxvTn92zs UXH3fofp1OuXh7UEae5vokTmAvbLzWRXNklMd0RgbcVdQoX+yfOgWRizxEl1iq8= X-Google-Smtp-Source: AGHT+IGIwf3MFbsmI5L/nn36QcLF0tw2dizRywserImseYHkgoEBii3QkvkJxB3CCAhauX+R+NVkZg== X-Received: by 2002:a17:903:41cb:b0:1fb:90e1:c8c0 with SMTP id d9443c01a7336-204f9c50d18mr13883795ad.63.1724824193604; Tue, 27 Aug 2024 22:49:53 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-203855df0c4sm92092495ad.157.2024.08.27.22.49.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Aug 2024 22:49:52 -0700 (PDT) From: Charlie Jenkins Date: Tue, 27 Aug 2024 22:49:14 -0700 Subject: [PATCH 08/16] x86: mm: Support MAP_BELOW_HINT MIME-Version: 1.0 Message-Id: <20240827-patches-below_hint_mmap-v1-8-46ff2eb9022d@rivosinc.com> References: <20240827-patches-below_hint_mmap-v1-0-46ff2eb9022d@rivosinc.com> In-Reply-To: <20240827-patches-below_hint_mmap-v1-0-46ff2eb9022d@rivosinc.com> To: Arnd Bergmann , Paul Walmsley , Palmer Dabbelt , Albert Ou , Catalin Marinas , Will Deacon , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Muchun Song , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Huacai Chen , WANG Xuerui , Russell King , Thomas Bogendoerfer , "James E.J. Bottomley" , Helge Deller , Alexander Gordeev , Gerald Schaefer , Heiko Carstens , Vasily Gorbik , Christian Borntraeger , Sven Schnelle , Yoshinori Sato , Rich Felker , John Paul Adrian Glaubitz , "David S. Miller" , Andreas Larsson , Shuah Khan , Alexandre Ghiti Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, Palmer Dabbelt , linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, linux-kselftest@vger.kernel.org, Charlie Jenkins X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2933; i=charlie@rivosinc.com; h=from:subject:message-id; bh=/49vITxLNj+RJRh4dWyqxf17jS8h791m43d/wmsbi4g=; b=owGbwMvMwCHWx5hUnlvL8Y3xtFoSQ9q5XfH5mu/c9Rrzlz8/vq1gj25AnkDItFvB5j/EjQ7d2 dgpWufVUcrCIMbBICumyMJzrYG59Y5+2VHRsgkwc1iZQIYwcHEKwERiehj+Oxm+8In6am8suLjK Qbj6ukzK61nfEx9q7X3ecc3HVutSBcP/QCmOFDVPG+uvAsGmtk+sN15sO6WrIvn+0Hvj8ITPYtt ZAA== X-Developer-Key: i=charlie@rivosinc.com; a=openpgp; fpr=7D834FF11B1D8387E61C776FFB10D1F27D6B1354 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 3166FC0015 X-Stat-Signature: mnwms376jdcij3zm1o86wqjxtc83g4qh X-Rspam-User: X-HE-Tag: 1724824194-267933 X-HE-Meta: U2FsdGVkX18eDDS2HQ2QMCz+aYS5xH5n0YzFkxU9DyjkeMElT8BbgvU3hEKgx2tNPjlvWBDfaxnTTTY31qIu3NF10+Y/d9KR8A4UN2JBvOZwQ5SY8TNOCIqz829e+oU+dw9hPRfE1EvOHd/Zo0X2yCvAbp5VWJyzx/do2+2+AB8MtoPv9wcbJv1j/WIIPpBkJjtFeHGtZ49FDaoMaqsMLXYbnBVxqrcB+PTlSxE17hBXwI0EMSKraKh5I+H97poMNlXYp6iKEbROEOp0bVdtjrxmgxU1ynv9bmyAksi4fAECoFiQ/kthEk4zatNbuARrG6pXdYk9sRXUwBoZQTDIL65N8dt+IJsqcseSM7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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: Add support for MAP_BELOW_HINT to mmap by restricting high_limit to addr when the flag is enabled. Signed-off-by: Charlie Jenkins --- arch/x86/kernel/sys_x86_64.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/sys_x86_64.c b/arch/x86/kernel/sys_x86_64.c index 01d7cd85ef97..fa16b38f3702 100644 --- a/arch/x86/kernel/sys_x86_64.c +++ b/arch/x86/kernel/sys_x86_64.c @@ -86,7 +86,7 @@ SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len, return ksys_mmap_pgoff(addr, len, prot, flags, fd, off >> PAGE_SHIFT); } -static void find_start_end(unsigned long addr, unsigned long flags, +static void find_start_end(unsigned long addr, unsigned long len, unsigned long flags, unsigned long *begin, unsigned long *end) { if (!in_32bit_syscall() && (flags & MAP_32BIT)) { @@ -106,10 +106,14 @@ static void find_start_end(unsigned long addr, unsigned long flags, } *begin = get_mmap_base(1); + if (in_32bit_syscall()) *end = task_size_32bit(); else *end = task_size_64bit(addr > DEFAULT_MAP_WINDOW); + + if (flags & MAP_BELOW_HINT) + *end = MIN(*end, addr + len); } static inline unsigned long stack_guard_placement(vm_flags_t vm_flags) @@ -132,7 +136,7 @@ arch_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, unsigned l if (flags & MAP_FIXED) return addr; - find_start_end(addr, flags, &begin, &end); + find_start_end(addr, len, flags, &begin, &end); if (len > end) return -ENOMEM; @@ -166,6 +170,7 @@ arch_get_unmapped_area_topdown_vmflags(struct file *filp, unsigned long addr0, struct mm_struct *mm = current->mm; unsigned long addr = addr0; struct vm_unmapped_area_info info = {}; + unsigned long task_size, mmap_base; /* requested length too big for entire address space */ if (len > TASK_SIZE) @@ -198,7 +203,8 @@ arch_get_unmapped_area_topdown_vmflags(struct file *filp, unsigned long addr0, else info.low_limit = PAGE_SIZE; - info.high_limit = get_mmap_base(0); + mmap_base = get_mmap_base(0); + info.high_limit = mmap_base; info.start_gap = stack_guard_placement(vm_flags); /* @@ -210,6 +216,19 @@ arch_get_unmapped_area_topdown_vmflags(struct file *filp, unsigned long addr0, */ if (addr > DEFAULT_MAP_WINDOW && !in_32bit_syscall()) info.high_limit += TASK_SIZE_MAX - DEFAULT_MAP_WINDOW; + if (flags & MAP_BELOW_HINT) { +#ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES + task_size = task_size_32bit(); +#else + task_size = task_size_64bit(0); +#endif + /* + * mmap_base is defined by PAGE_ALIGN(task_size - gap - rnd) so + * subtract out the task_size to isolate the gap + rnd. + */ + info.high_limit = MIN(info.high_limit, + (addr + len) - (task_size - mmap_base)); + } info.align_offset = pgoff << PAGE_SHIFT; if (filp) {