From patchwork Tue Dec 10 02:41:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 13900717 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 CCB24E77181 for ; Tue, 10 Dec 2024 02:42:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 39FA66B0103; Mon, 9 Dec 2024 21:41:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 326AD6B0106; Mon, 9 Dec 2024 21:41:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0DD936B0103; Mon, 9 Dec 2024 21:41:58 -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 DBF6A8D0001 for ; Mon, 9 Dec 2024 21:41:57 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8A64E120257 for ; Tue, 10 Dec 2024 02:41:57 +0000 (UTC) X-FDA: 82877498802.10.C493283 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) by imf13.hostedemail.com (Postfix) with ESMTP id 6AC4A20005 for ; Tue, 10 Dec 2024 02:41:34 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="Wk/cBUGe"; spf=pass (imf13.hostedemail.com: domain of 3cqpXZwsKCGcPFQJXMXNSLMLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--kaleshsingh.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=3cqpXZwsKCGcPFQJXMXNSLMLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--kaleshsingh.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733798495; a=rsa-sha256; cv=none; b=GskBXaQABJ8P81fENgJlayduppjq6NpwL7hnCcTEsDRCdVHtpX4mHYkabD+qdtXPqBPL4U AAn8BqNSBcavVxByi/GK+BeM/xKrDkuiZS06lSOFFsiRaUt6PAgCZl8tGhsl7sZtoDcT0G bjQJa9D96nFhUWjdiGTqnMiC6QYvV78= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="Wk/cBUGe"; spf=pass (imf13.hostedemail.com: domain of 3cqpXZwsKCGcPFQJXMXNSLMLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--kaleshsingh.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=3cqpXZwsKCGcPFQJXMXNSLMLTTLQJ.HTRQNSZc-RRPaFHP.TWL@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=1733798495; 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=mJfesLSvigtPKLpc5l88d8L2TdNCPAh9XHgD9Aaia2I=; b=FcqeuM1pA0cESyEx1/MckEpOqI4SJNufkTyJvxDSPYUXtMpm+1ri6OWZFosDtZjQTNdi8x hz1tm4wewJtYTiGFVYQP+Ae6L5I9/ZfP8vvaqr/o9bwsIVjo8wJDL5A6oy+frCAx9Ca4Lj smTZwD3QRbMM3WWwWD4TO5NGq+s6f14= Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-725e8775611so1558901b3a.1 for ; Mon, 09 Dec 2024 18:41:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733798514; x=1734403314; 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=mJfesLSvigtPKLpc5l88d8L2TdNCPAh9XHgD9Aaia2I=; b=Wk/cBUGejLwZaV97pR7grG1+howu9gNE+4AwntEN5D3qobC7f4uW8LT6/gEipy5fHj WKbPjkp4gbeNNB/d+RLv32CRte0ZmFiqpV36jAJ4ANsTisljJcu3ZfMA0g5xySlZVhy9 7A/Fr1F2VIbOZO+CS/iHe32q/RO0MUh+QYJF+73RZXQIpwyeWRsndJV1eXEpjd7uKAIX AFOZxhjrcP0IbDl9hWL3I+i6vOlaVnAYyFi72jxrOq21io5WgxzWpnXw1QxoA6ksVsKO oUFrvc0BdV0mN/Rr2iq/kyCvtX8v0gGFbk6YxelRWYkYshtY74F3A1mb7Q0XaWSfahqo fwFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733798514; x=1734403314; 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=mJfesLSvigtPKLpc5l88d8L2TdNCPAh9XHgD9Aaia2I=; b=r8zYTHfCLFfNqQqXxWJ6S98nAXxUgOt6JWCf1soFm3+cKdKc9X7XaA/zAhZkLZcWQ5 MRr6PgCuxRPGGVS3eUHFQuEQ0r0WCcbvCl7vunQbneDrL8QT7LO0ke83LqcJSahfnI5E Vp31Ixtpnk9W7cw9Q0+e6SSvU0SFRRHfWwJnZvh7IMcox1qAned2aVHnwFmpNFtU+Upm GzJ0XTxvz6J6x6Gn2vYgL2cDWSkBn/3mh32OL5uVZoRX4dDhcp/vBFmgxnvvD4Mjou2O 2t9cUg4C30LI+QLB7RSdPfGU46SGTpo6zu5G6JRelMNtGf6j7Xr01vA0bBpzQNE9+JRt t0tw== X-Forwarded-Encrypted: i=1; AJvYcCU63HuEJd+QDjhVqdg+zCpGwwSkzOOW+B/CI3ZQs/bUcaj/2x0Cz+iaVpV3SCKfcEWMwCzouo3r+w==@kvack.org X-Gm-Message-State: AOJu0Yxh6bRSHQOWeOjZfVJsVFhkBVRDgs+MdsC0wKwkZCVGOmJUsFuK F7P/DfPrPsSriDHfYPUZT5GJzhG44FAA71tstUnbmIj2qm02A154c8h/tBDFKn2mXl0pz18wPrg sUV2wlJji1bzYnG2JFJlSlg== X-Google-Smtp-Source: AGHT+IH7c/kAWkG1dgCDsYvw4E2EH4mgzAeRRpKy/JiEpi/blwtzY1No/+Nej6iG3SIgIjXbM8HkPN0PZclD3pT9zw== X-Received: from pfbf13.prod.google.com ([2002:a05:6a00:ad8d:b0:725:a760:4c72]) (user=kaleshsingh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:14d3:b0:725:f097:ed21 with SMTP id d2e1a72fcca58-7273cb1d8c2mr3121572b3a.15.1733798514446; Mon, 09 Dec 2024 18:41:54 -0800 (PST) Date: Mon, 9 Dec 2024 18:41:19 -0800 In-Reply-To: <20241210024119.2488608-1-kaleshsingh@google.com> Mime-Version: 1.0 References: <20241210024119.2488608-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241210024119.2488608-18-kaleshsingh@google.com> Subject: [PATCH mm-unstable 17/17] 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 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: 6AC4A20005 X-Stat-Signature: 8ub7x673x9a43mc65tdmmy8nshzyggyo X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1733798494-746740 X-HE-Meta: U2FsdGVkX19g7BBB3mgc1xwrr5RqRsgM1lXnqGrr1i257XpMkGoAxQdfdQfLqCQKRjd9SlWO5pJ9l7Dm2Gw3BQBl/3MSkXqSNqlvYDqIq7h1FwAAi3zN2QvwMMzyKcyLAp0ap6hL8MCyubyrkthG2otrQXOjbtRtqDy5wvrc70WzjydQwanzRnFF8HSTPfw0TyiysP/QoPSictQiIvqWMFBTOSZbifiBfzycCBJ3TuLB+MmeEXA/66qrrpidxUgQ6mWBHgNMBXBCYHAx664q8W9NTDJscfUBKEyvEypvL9FcjohsmrLRgRzR7XfELBdIr0kevuTkxVQK8vfOMrkb3CCpAXuHPhKzxqpHTBZliHfLbF4cUwEcnFwFusr2wL7jzFGlVNz/qJtxhBUw3mgJADoqBPiThfdg7SW9wr7U1tmzKeKo60MqD4UMlSFe3tqc365b2gVTaAv9PQ/Nu+bDA6z8PbBn7BurHUBXliQzWpJp4uVYGSVcki9xBYieifY28YG7WgxY9STCpSe76Ql7LKKiPtEiyHgILPUMf4eBB7/qFHRMI6znQy353zj71Ic1bFadsvu/WZjvxiBODmvSa/p8AqUC68TljrANGlEaAnjsTAmNqR94aKnel5VfjRHsndA6EHCWhhpFj8r7892+7mgSWTNyldcmp0KDj51F9l0gfdgcuArkfXwQl6+me6TlOJAf9TZYYK9O7W2V6ojvEcVt3RJ8Z53dcFlLjQPuZvIeuMnRUYYWmE2BvifhIjjMimPwUcADeLmSfBXfNfWOQwEfCLejnNDJ7Rd7UnHhupapYJUBiCyYvaAzui66PHD8PEVU+KGjN/xJO+Fyn1hAFFNEssqvpikiko3mDB42qWNvS1l8mx7FZM4oF36FCKaJvZLY/Tx8kkSbgesum7y6KxrZVbEABBRMXDQQuLMB+4s+qpZ9h7CM/uEuiyQtYv7lWEsjidydmn2o51EgqGs IKis1ZzF a+kTTBEqV1WD7NzXEvSDiet5SXmyq5NUsZ51lIRhyoNYYywZNuuCBKyLstAJTgTZvxeQznOCnzJGYOwJsAFiKcNfalN/HYa/Q+vRaeZmQuWm1zRkU0eV2kHSpWXEtSmachXMCsFYVw7dzahf6hhhPaStwbg9bpiSY2C/d9BcJCTQ/81xKIrq82gneK858YcxSgSgGY3BJrJpqz/MkrJnZWXWdSPSI4M5r6srqCG5UKLbGrTdLvuo8cFMzo9SkFuKnCwA0urS+9Q9zJm3SCxjJmZPl5dxAx9zH4iVIbFiLliR3UOGdM4ExKJUD+1UYU0nS6VM6du7kYPlloaSuFtEoJPHHNpw+tvmxujOs/hEL4y+8DCJsCzF892zvSlMOoP65Rz56KeNHYzdSkgWFfZxsI0twOgpIsduUGgLfo+y82BPPsKDff3VuhrdHsvLFYjeZwPpvOj4oL5/a35fNhF2qoqaihyUmce9esds2Ic+cVkFrppCkxLmiXbrQYAO/sFmwJHkV1zHgjYeGA1ySzDCZ2JWTKz0Q0+oSSiZpGJR+VSk0PK+cgn5L4SyA/w9eGhAKtE6MAL+/gs/dU+oJ4jUsVUuLBA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.015945, 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 that fails. Signed-off-by: Kalesh Singh --- mm/huge_memory.c | 15 ++++++++------- mm/mmap.c | 1 - 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 137abeda8602..f070c89dafc9 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1097,6 +1097,14 @@ 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. + */ + 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 +1125,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 59bf7d127aa1..6bfeec80152a 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -807,7 +807,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,