From patchwork Wed Dec 11 23:27:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 13904400 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 DBC93E7717D for ; Wed, 11 Dec 2024 23:28:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CE28B6B00AA; Wed, 11 Dec 2024 18:28:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C8FA16B00AB; Wed, 11 Dec 2024 18:28:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B09366B00AC; Wed, 11 Dec 2024 18:28: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 8E4366B00AA for ; Wed, 11 Dec 2024 18:28:31 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 39CCD814DD for ; Wed, 11 Dec 2024 23:28:31 +0000 (UTC) X-FDA: 82884269160.04.EBF0E92 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) by imf12.hostedemail.com (Postfix) with ESMTP id 0BD2140018 for ; Wed, 11 Dec 2024 23:28:18 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jPLp1vWB; spf=pass (imf12.hostedemail.com: domain of 3HCBaZwsKCAcrhslzozpunonvvnsl.jvtspu14-ttr2hjr.vyn@flex--kaleshsingh.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=3HCBaZwsKCAcrhslzozpunonvvnsl.jvtspu14-ttr2hjr.vyn@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=1733959691; 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=oHbDZ++mGE8tEdFCxijZAS2pZkYuZT5989Fg2aOx0oY=; b=jbxzlkrFRzLCeRntaMY3qNT7t5WjsjSqa8BHnZcfjBcQFU3CBT1RUYdvyVFbnFYqsm/CaZ TMEcoOEWtf6wNmMWwQPEZjY3+xhrc8sO6mu+dBLjqmP26uS5R/VVa5T39ISFojvJYw6/pq tvvXH/CjJivPJigTyZWfXk1sCiF/+7o= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jPLp1vWB; spf=pass (imf12.hostedemail.com: domain of 3HCBaZwsKCAcrhslzozpunonvvnsl.jvtspu14-ttr2hjr.vyn@flex--kaleshsingh.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=3HCBaZwsKCAcrhslzozpunonvvnsl.jvtspu14-ttr2hjr.vyn@flex--kaleshsingh.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733959691; a=rsa-sha256; cv=none; b=G82VknnHvoVn4okow1dmOh4oQ/xCZiCWZuvyDdHwJiOgF7s3Xo07zt9cxikR2z5nBKzlAp Rd+75vukr3gju6I063Fq35sIzImZM4r8/bofy1jgRpCEQC1irg4rYTDK6bzl0n8g7RBFOg x6BacUxpaOxidBsbKymKAkHFcQ5/bp4= Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7273a901821so13370b3a.2 for ; Wed, 11 Dec 2024 15:28:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733959708; x=1734564508; 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=oHbDZ++mGE8tEdFCxijZAS2pZkYuZT5989Fg2aOx0oY=; b=jPLp1vWBrWfRD3xa7dCChxVo/uxKEFdVgQjcz9a9cnLbZsT5rjr5nCTxkwizzmmMIT OHfb6/XZz1byu6PnGepMBnnoC2hy5fu+liNrrNNd354srKb57woftdHHrftrW1d06cVP DoYKOaADa1DKZY7Dl0MT7oPs4PnVtYdvDWAZoJ4kdVuw3oy46hEbYhISFUt4mHTLxhPX Zxh8+xm+P81EAfdU1PmrhHaeNvVnf0khhaI41msAiIGH8qoXkKvf5y91dnkj8kgzaZ6C AGOvXWuFQUiKG0iU+6YYUC35fAl9kNRmFT1iFqq9JJhhbo0K/HMuRl1jRDm3TUyqITyw jujA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733959708; x=1734564508; 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=oHbDZ++mGE8tEdFCxijZAS2pZkYuZT5989Fg2aOx0oY=; b=HTJRJZmgP/MuyXVW7ks9D3NJRZp3t7/K4lrGJKszls7eCTayQDr4vtKq0gdx87NPfS YGjFpLKpc7gXFZdhfhbNH0FO7S1zbIis4BRFDNgvJcjYjnz2JH9rnyv55k61HC6gwmxJ rEdCgNRUIKMPm1YFeP1ERJiqY9SuE8EoB26uzTM6OHqt+zeIWnx2QNUHtDeLinwqzwTG 4F6ht/nviLmv2hqbDcbdS8lwFqDc6kkyYH89JSNWzpCoYeLQF5/UkO7Qx22gJklmUec6 5d7WbCbia08a1BD1bQCzyeXCfdFkXV8HUQAkXCeOLyuZrwrS7c4ETU0XzXEcQ2rywzuv iGRQ== X-Forwarded-Encrypted: i=1; AJvYcCVb5BGknpJlhv8BPef0tOf3afD5IcI0SJOBfWxIBBlRffC325YLyfCi7Itj0OJ+xgY+WNxg+pEM0g==@kvack.org X-Gm-Message-State: AOJu0YzSm0XkINPI5GalUl54NW1QI3LI/I1md72WzHt7h3JtxoEsSDMC PW13H/KjKb4SlGPKv/jsYDMDnaNjNHfXdJeeL4mc+JgI1dpggWHPf5xeCAbZXi5ncHfrxNDnFZn YlGTKfNDLyphqJX0gXpmh3A== X-Google-Smtp-Source: AGHT+IGlmctwmnr0LOvk4eyOoUS2Mz0NPFGCSegKhSqs8wGodxRb0W3o7XrsirlslhQ66CcLZRwSCG2H88EXSdEssw== X-Received: from pgbdl12.prod.google.com ([2002:a05:6a02:d0c:b0:7fd:1eba:c2dd]) (user=kaleshsingh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:150b:b0:1e0:d89e:f5cc with SMTP id adf61e73a8af0-1e1ceaabbb2mr1312325637.11.1733959708164; Wed, 11 Dec 2024 15:28:28 -0800 (PST) Date: Wed, 11 Dec 2024 15:27:54 -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-17-kaleshsingh@google.com> Subject: [PATCH mm-unstable v2 16/16] mm: Respect mmap hint before THP alignment if allocation is possible 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-Queue-Id: 0BD2140018 X-Rspamd-Server: rspam12 X-Stat-Signature: 7krqgyisnroy7ifzmo47m5irshs5zepf X-Rspam-User: X-HE-Tag: 1733959698-572303 X-HE-Meta: U2FsdGVkX18z8nAFMxrOT+1obl2nxAMLiY+bYl+IR8sCiI72EXHv/l3IAYkcyp0jB4o/b3bk8z6HrnSIz/Sq+OGJIeYdzVeMMR1nkLGvT2tys/dp6UCiFWqDI29nMxQxqedXQ2YBfCUAqz2VgpSyy8ssosJJ6csIKkA26QcGEzzVFpO+a7AhxTTj1fXcWGwCRUZLWSOuI6kKhbAYR07vCLLqAxRZeulgBYyKOoblU43YIBKfQbVJPTezdsfV7pNmFFdBWCB+uAiKFwvMHqr61mhRQJmWJ14+blDYgcr8wF/QD82iws9JHMtXfAyxbCR/vFH8+Ey1KOknnmFAlicHu/C/76aYUnCYUCILdpxrUh7hHTpBJau1muhL3/Kb6eZ6CKh7j+s6isXAIDL4DbW5z2oHUMnkG2gthiDn52lu0wcEerjNUzUZII8oep0eR/Ap2wFJNdy0kKIzu+UqgIftj8UGg6zesaUNiaMSDpegqMD1reM+MxMDGaXwIBLWJ1+n+V4tdzGraoxpmvFFijEqgQXhKmtgFfYSjuIuC/spe1W+mb9oyyl6bm5TwdeR/F1Zt3K/ei/fashF4gHLzdQK1cIuUnD5RoDDvrDT9krvbPR8hC9haIBSDEVKj18QG5uPp8RIfvIBBRxtns0pN2lTnijnpF5QwB1GGUSedPspVLBhm4EnOgmnsqkjBwhHlQx/9gybxRuuJYb5DzVx9NhH9Wnhsy0qXHPsHD8qXhX+2SWetzE7JANPkCPfyDvd1FbLcCfrxO5xKnpigQ1WHrtkt/cCVOgdhoRPLXrBqt/oYtvaIih4yVrhB1PEyibUGL+twf/QOmjO8eObt987BruVliSPDCRi2YIwHEH3yQuwb0fzxr1dXsGTiYbRy7hYKE9zqtvdF3siE2YEQYjyAYDge06v2KLYcvA6fH8Nzj0dMWwD3lZ5Z+ImtfrNE7CS3U6N9JUomn3yGQ8fjHprfnd lzr/dUY5 3GLuZ4r9wkcvOxXzptWTY6i90FwHZB6aMX1LFYV4i/3R7q4SJ7ypDCRIUKx2xN0upF5Ak8AaDzzl9xW8WMjimjd9iUoyzm9zlQwaNJm8swSQ+OihQ26pS8+e/w86LtQkOr7RIvf67ZB7yAxcxKZ72sB4w+zNC8qr/9CLBk9EwZaNOTUoZok7yHGq2OqdH17KRc08H8A2XmpaST/q0t3mPyx3cpr+FdHtAGt7shdVs11qv8IPNesf8ZfHSM2/8bJmmWDIjNLDjS7GL/Hw/nCjEZsCZB3deLG7K88LPv757eekT86j1lzvkKl0/kzcl08BhTPuJ4Y4xBHXf+1M9osASF7PKPPLI0QCihCPuOdk0kxnKcG54/lmNUphPpkjigMnkxQeoNS+QTWm3pFnfB/v5s62n8iYntYhUBlvKu+VehuHj7iWqWRhfdY4vR4W67KWa+rHcZz7guUbfsXv+tD5miU5IQEmJUPl9D+RiX1weI29J0pvoQ++kUd9A1NsXxxn3bqxE06n317GSRXfXAdPK4RXm1UkxudzNiIZIu7PI5zsyhQrid79L3Cb25JnHDGZD77WIjwthA9xR2jDCiSNwEwocFA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.057586, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Commit 249608ee4713 ("mm: respect mmap hint address when aligning for THP") fallsback to PAGE_SIZE alignment instead of THP alignment for anonymous mapping as long as a hint address is provided by the user -- even if we weren't able to allocate the unmapped area at the hint address in the end. This was done to address the immediate regression in anonymous mappings where the hint address were being ignored in some cases; due to commit efa7df3e3bb5 ("mm: align larger anonymous mappings on THP boundaries"). It was later pointed out that this issue also existed for file-backed mappings from file systems that use thp_get_unmapped_area() for their .get_unmapped_area() file operation. The same fix was not applied for file-backed mappings since it would mean any mmap requests that provide a hint address would be only PAGE_SIZE-aligned regardless of whether allocation was successful at the hint address or not. Instead, use arch_mmap_hint() to first attempt allocation at the hint address and fallback to THP alignment if there isn't sufficient VA space to satisfy the allocation at the hint address. Signed-off-by: Kalesh Singh --- mm/huge_memory.c | 17 ++++++++++------- mm/mmap.c | 1 - 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 2da5520bfe24..426761a30aff 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1097,6 +1097,16 @@ static unsigned long __thp_get_unmapped_area(struct file *filp, loff_t off_align = round_up(off, size); unsigned long len_pad, ret, off_sub; + /* + * If allocation at the address hint succeeds; respect the hint and + * don't try to align to THP boundary; + * + * Or if an the requested extent is invalid return the error immediately. + */ + addr = arch_mmap_hint(filp, addr, len, off, flags); + if (addr) + return addr; + if (!IS_ENABLED(CONFIG_64BIT) || in_compat_syscall()) return 0; @@ -1117,13 +1127,6 @@ static unsigned long __thp_get_unmapped_area(struct file *filp, if (IS_ERR_VALUE(ret)) return 0; - /* - * Do not try to align to THP boundary if allocation at the address - * hint succeeds. - */ - if (ret == addr) - return addr; - off_sub = (off - ret) & (size - 1); if (test_bit(MMF_TOPDOWN, ¤t->mm->flags) && !off_sub) diff --git a/mm/mmap.c b/mm/mmap.c index 76dd6acdf051..3286fdff26f2 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -814,7 +814,6 @@ __get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, if (get_area) { addr = get_area(file, addr, len, pgoff, flags); } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && !file - && !addr /* no hint */ && IS_ALIGNED(len, PMD_SIZE)) { /* Ensures that larger anonymous mappings are THP aligned. */ addr = thp_get_unmapped_area_vmflags(file, addr, len,