From patchwork Wed Dec 11 23:27:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 13904386 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 08113E77182 for ; Wed, 11 Dec 2024 23:28:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 910386B0089; Wed, 11 Dec 2024 18:28:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 821E76B008C; Wed, 11 Dec 2024 18:28:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 69DA06B0092; Wed, 11 Dec 2024 18:28:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 49FA96B0089 for ; Wed, 11 Dec 2024 18:28:05 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id EC092AF607 for ; Wed, 11 Dec 2024 23:28:04 +0000 (UTC) X-FDA: 82884267564.25.C9A2F28 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) by imf17.hostedemail.com (Postfix) with ESMTP id 231CE40002 for ; Wed, 11 Dec 2024 23:27:44 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FMdtn1iG; spf=pass (imf17.hostedemail.com: domain of 3ASBaZwsKCOoWMXQeTeUZSTSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--kaleshsingh.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3ASBaZwsKCOoWMXQeTeUZSTSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--kaleshsingh.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733959658; 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=C3IQSr9A1qLdVskR9rEe7ppqc2lFkJy3Q6Qw5iPRaPE=; b=hVV100tYfSKwa66g/YHfwxT5nf9xon/LFxI2v28HpgWbRjRsW1EgbYPWn45W9sPnrquboa 88R2BOwyqy0WY/DhA5R7ZoHwwW1Xx1Uk1PNbntPxV2TDmcjN9qKQHnq8GeO709HbRnZB8i nxHldGplVEOSQHYlk9g167Xd6WIO53Y= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FMdtn1iG; spf=pass (imf17.hostedemail.com: domain of 3ASBaZwsKCOoWMXQeTeUZSTSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--kaleshsingh.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3ASBaZwsKCOoWMXQeTeUZSTSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--kaleshsingh.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733959658; a=rsa-sha256; cv=none; b=RuHwmXREuCndwSnghwLNJI9SYXo2PLRMvbmwzxGXmEdxjLiQ1HH8hLqc6JvdoXw8dao8cf sIbyT38NHvxspfBN+u4FTJGxrdrcyE30QHGWTTe+77mdaCNFuB/75f5OTgApoLXY30oMTN KIedL9R7+Uc3VhsEHgQnA1ytgNMQk+8= Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-728e7c06768so19658b3a.1 for ; Wed, 11 Dec 2024 15:28:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733959682; x=1734564482; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=C3IQSr9A1qLdVskR9rEe7ppqc2lFkJy3Q6Qw5iPRaPE=; b=FMdtn1iGpojLveTj1CQ/p5JIjXwXFxLCo9TmL5FQmjmsRMZdxdYB2jZpreU9noyXrl wHW7OXpYLW6v1J9a8IhAjglj6q5qDFJdNTHFl3333KC0PWjkBwJap21wLkB90T0yecNJ 8UngglFcfeiWC+SfVz4OlErxm+EUK+RJRMxzNzaEijr4LCIfAvB6VzJ6yk0EESpUVSCJ s6wZTCQ5OMTMiJqITQP4liRSAsX6eiLqhsF4JqgXkEqQj7XSyhbl692d85wuLGXeH7DZ Ttzb0RjU3BI65XCWwjVfBB8sHkLts9kq1NPAaBTRMKclJpVduzLitx2DqW7kCwzN/aJn 3Pgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733959682; x=1734564482; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=C3IQSr9A1qLdVskR9rEe7ppqc2lFkJy3Q6Qw5iPRaPE=; b=uAXoBtFbLWty06MsvuOZaawenDdqsYVDjm6/g6Vtdm/7psH6eGKjIxKocmwtwWs4EX AeDuMCvrVfw//bqzAEiuaZtciizVk53YIa4jaApMtKbLEnlRpLpqWWB3+jGGtGrEkf7f 8OzS6XQAYEaxrozmOMEyh1K2q8l+r9drFgldWBQhfstyLfaS2chMukN05/ysqZoRbcQn yUT8CmtraECXuLkC6+bYVeA7qu+R39VUhfXhcVelMocQ7lxnVfkL/1miHQFNN1Ss3E+Y HANGTLlKK6xUUdp4hWwnHUULyTKRFFmatbGC6WYTGpI73pPZ1OMbKovaGggy5hCNT4Jr VgjQ== X-Forwarded-Encrypted: i=1; AJvYcCXbpK7ScuZ/wnJbU5NvdQ4sUhOACqaVIg87cC+aRyycZgE2x6cXUACcBYggD+RAxhp39D0xmJ4Lqg==@kvack.org X-Gm-Message-State: AOJu0YzufbOAZ/d/pSTGzqeaszTJRZUT4xuKgkXdT0GqXyeUvOaeT4nn ZWkM18/YbogQElgsg4kfER+wL9ewN99uaZ+u2+E3IlTGgHu+89lu9iPizd/GxI0AgZU/ehzlYHS L9IVdla2MwtJaTepSbGfYAw== X-Google-Smtp-Source: AGHT+IHwoWCnSZAFhgfRGzh0vJKMetndx4pRL/iGA/i3q5lPp5GtpRRmcU9xCr75DNYXJvBdH8Wl1Hh5YkFK8klCiw== X-Received: from pge13.prod.google.com ([2002:a05:6a02:2d0d:b0:801:9432:2d6d]) (user=kaleshsingh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:cf83:b0:1d9:18af:d150 with SMTP id adf61e73a8af0-1e1ceafb16amr1741157637.21.1733959681795; Wed, 11 Dec 2024 15:28:01 -0800 (PST) Date: Wed, 11 Dec 2024 15:27:40 -0800 In-Reply-To: <20241211232754.1583023-1-kaleshsingh@google.com> Mime-Version: 1.0 References: <20241211232754.1583023-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241211232754.1583023-3-kaleshsingh@google.com> Subject: [PATCH mm-unstable v2 02/16] mm: x86: Introduce arch_mmap_hint() From: Kalesh Singh To: akpm@linux-foundation.org, vbabka@suse.cz, yang@os.amperecomputing.com, riel@surriel.com, david@redhat.com, minchan@kernel.org, jyescas@google.com Cc: linux@armlinux.org.uk, tsbogend@alpha.franken.de, James.Bottomley@HansenPartnership.com, ysato@users.sourceforge.jp, dalias@libc.org, glaubitz@physik.fu-berlin.de, davem@davemloft.net, andreas@gaisler.com, tglx@linutronix.de, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, chris@zankel.net, jcmvbkbc@gmail.com, bhelgaas@google.com, jason.andryuk@amd.com, leitao@debian.org, linux-alpha@vger.kernel.org, linux-kernel@vger.kernel.org, linux-snps-arc@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-csky@vger.kernel.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, linux-mm@kvack.org, kernel-team@android.com, android-mm@google.com, Kalesh Singh X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 231CE40002 X-Rspam-User: X-Stat-Signature: uaz3y9uerdtsnmaeqz4i18oh6wxyfq88 X-HE-Tag: 1733959664-485144 X-HE-Meta: U2FsdGVkX1822MShXcST8A6BOs6ngnUzLbZ36Zat2/qKnHjEd4NzLA1vz7nCfbTIMVlWzN3/dW7DaVIDk4M1S0eFho4x11rB6NFEp+1RAb2Gjc3MQsT4swC/vcqlzUjkN/4JkANKUu7uHRFo4n2eTUux6OmxMgoDaGEeVTStjLKbYUpKM+ObPpS05wny5/JrKh/DlFw3diSn6H7cyPB4ki7AUoCLgm1maTPiWZkD1Q5pdDaJ+ddxPrRyzLilWIbLMlgKseuP1dY3hXKnL9GtxUq3TuFimUs+5X9+RMsjzIZtA1MpkpfsB1la/62xO1UhVbG3U0aXoX7ZQ+pwCHflOs+jBI5Ik5NCFsKWuJYU3C8TMtyR71iynA6MQcabOEcxmc8hwFMYd0WnbjDH/jcD4B3u/0JBc0jyy7r6XIqINRIhzJSjcatlVbJy3ixqwugEiXDiBVGZvTu+RU47h5Q/AU4LmgupL/SJp2wvNhGIRN1rMiFr0xp/CgJwMoOuGHCZOcNZ2MI/THtnE9ikBmPXiz7g6/un9Gz46GilsMbiBXsIwVfp1SsSCojrqj5OA692HDbwjoVGQRrhE4S6cwrR/liw23LmwATQSYuN1B2MGjkmQIi7i2IIr7qq3DSrv9TaIZrJDatdMQ/GIe6Wq3o8N54OxNVDTSxQ3MMaZDIlyVi1/7U+MfpLpJrTE8PJQAdvhK1dlhNt6s6K1NDmBS7EQIEXQii2P8sJo5+6qlLLtVAJIc0RvnUd5sVKI9Ckrj28X4R0cuUsDzz4YSJiCysxq80HM3tnTnY8wsg4KE7Ss2loYeUrnokX1jIvuphRQYx6YHCeG7IIBs3Tcf2PcHuPPnjefJsKTqRlua67nx9wq1jOxV7iBY4Evzct48dBVYvnpqoGR6ZmPi2uoAmgDE4oLwjgqG0jJk//B8imZUdc/+gGIBx/2ICONjJwQUZROizUmQvut4SPIBKIINO6HZ7 ilGAjuLk rNhWaMZpj7oiw2YVlitYbd8ZiUxm32DrVPj6Sr7znsbHfi+bVx+nb/K0uzoiDIa2fK7eRRaQfWfVYc2XARUtPJ+dUV2skMI/0By48cvXu82sR3duAG8eBkXeESsmLajUcrVolDBnTowxLs4YXhJ8MUp9oCGWGpBlvQDqng626eio9Au++76LjoeJVTv5kA1hJ47P3FVCb3YVb/WuKtDtuh4b30cJ5i35si5och9i+OJpkk53tAo0wzziTgxGjsgc9wEOyGlmFXmBbpHmj/xG/CYIzrQYOyijIfo8ih9wm42itA1JjUwjTS77TKCqdpAxlvtGvhvzLepOtslEp7R7nG1gS+jg+FwW1n+798PC5U/9jPA8PhYbECsYAVenDdelVuqQ8MIwLabi8Y5UTUBlk5uRWUpRPX3QzGVz6pT4Xg6ljBcG3dUVqvzKErLbtjkM4ZBRChcFSYYL+vgwp5Evw65n+kBOLtZDhi2xptcRcy0HUhobPecYvQBb8VyjLF/47cltNF9ExL5jqZIPQhg1v6Nsb42+A7KF3yP+F+WyKeQ1pp6BwRU3sDr2DyTrZLthgoClUBCajgIj8aLIpMq6fxZF3lOhwM28YcfF9Q8s/M1d95XZDK6cRHrS5wZszuAPgkAK3 X-Bogosity: Ham, tests=bogofilter, spamicity=0.013145, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Introduce x86 arch_mmap_hint() and define HAVE_ARCH_MMAP_HINT. This is a preparatory patch, no functional change is introduced. Fallback to generic_mmap_hint() if an architecture doesn't provide HAVE_ARCH_MMAP_HINT. Signed-off-by: Kalesh Singh --- Changes in v2: - MAP_FIXED case is also handled in arch_mmap_hint() since this is just a special case of the hint addr being "enforced", per Yang Shi. - Consolidate most of the error handling in arch_mmap_hint(). - Fallback to generic_mmap_hint() if arch_mmap_hint() is not provided. arch/x86/include/asm/pgtable_64.h | 1 + arch/x86/kernel/sys_x86_64.c | 64 +++++++++++++++---------------- include/linux/sched/mm.h | 5 +++ mm/mmap.c | 9 +++++ 4 files changed, 45 insertions(+), 34 deletions(-) diff --git a/arch/x86/include/asm/pgtable_64.h b/arch/x86/include/asm/pgtable_64.h index d1426b64c1b9..4472fd0040c3 100644 --- a/arch/x86/include/asm/pgtable_64.h +++ b/arch/x86/include/asm/pgtable_64.h @@ -245,6 +245,7 @@ extern void cleanup_highmap(void); #define HAVE_ARCH_UNMAPPED_AREA #define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN +#define HAVE_ARCH_MMAP_HINT #define PAGE_AGP PAGE_KERNEL_NOCACHE #define HAVE_PAGE_AGP 1 diff --git a/arch/x86/kernel/sys_x86_64.c b/arch/x86/kernel/sys_x86_64.c index 776ae6fa7f2d..008c6d19bd02 100644 --- a/arch/x86/kernel/sys_x86_64.c +++ b/arch/x86/kernel/sys_x86_64.c @@ -123,31 +123,44 @@ static inline unsigned long stack_guard_placement(vm_flags_t vm_flags) return 0; } +unsigned long arch_mmap_hint(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, + unsigned long flags) +{ + unsigned long begin, end; + + find_start_end(addr, flags, &begin, &end); + + /* requested length too big for entire address space */ + if (len > end || len > TASK_SIZE) + return -ENOMEM; + + /* No address checking. See comment at mmap_address_hint_valid() */ + if (flags & MAP_FIXED) + return addr; + + addr = PAGE_ALIGN(addr); + + /* Fallback to VA space search? */ + if (!mmap_address_hint_valid(addr, len)) + return 0; + + return generic_mmap_hint(filp, addr, len, pgoff, flags); +} + unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags) { - struct mm_struct *mm = current->mm; - struct vm_area_struct *vma; struct vm_unmapped_area_info info = {}; unsigned long begin, end; - if (flags & MAP_FIXED) + addr = arch_mmap_hint(filp, addr, len, pgoff, flags); + if (addr) return addr; find_start_end(addr, flags, &begin, &end); - if (len > end) - return -ENOMEM; - - if (addr) { - addr = PAGE_ALIGN(addr); - vma = find_vma(mm, addr); - if (end - len >= addr && - (!vma || addr + len <= vm_start_gap(vma))) - return addr; - } - info.length = len; info.low_limit = begin; info.high_limit = end; @@ -168,34 +181,17 @@ arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr0, unsigned long len, unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags) { - struct vm_area_struct *vma; - struct mm_struct *mm = current->mm; unsigned long addr = addr0; struct vm_unmapped_area_info info = {}; - /* requested length too big for entire address space */ - if (len > TASK_SIZE) - return -ENOMEM; - - /* No address checking. See comment at mmap_address_hint_valid() */ - if (flags & MAP_FIXED) - return addr; - /* for MAP_32BIT mappings we force the legacy mmap base */ if (!in_32bit_syscall() && (flags & MAP_32BIT)) goto bottomup; /* requesting a specific address */ - if (addr) { - addr &= PAGE_MASK; - if (!mmap_address_hint_valid(addr, len)) - goto get_unmapped_area; - - vma = find_vma(mm, addr); - if (!vma || addr + len <= vm_start_gap(vma)) - return addr; - } -get_unmapped_area: + addr = arch_mmap_hint(filp, addr, len, pgoff, flags); + if (addr) + return addr; info.flags = VM_UNMAPPED_AREA_TOPDOWN; info.length = len; diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index edeec19d1708..f4143703f234 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -205,6 +205,11 @@ unsigned long generic_mmap_hint(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags); +/* See generic_mmap_hint() */ +unsigned long arch_mmap_hint(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, + unsigned long flags); + unsigned long generic_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, diff --git a/mm/mmap.c b/mm/mmap.c index 382b4eac5406..76dd6acdf051 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -705,6 +705,15 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr, } #endif +#ifndef HAVE_ARCH_MMAP_HINT +unsigned long arch_mmap_hint(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, + unsigned long flags) +{ + return generic_mmap_hint(filp, addr, len, pgoff, flags); +} +#endif + /* * This mmap-allocator allocates new areas top-down from below the * stack's low limit (the base):