From patchwork Mon Nov 18 21:46:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 13879114 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 121C6D60CE7 for ; Mon, 18 Nov 2024 21:46:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6F3246B0083; Mon, 18 Nov 2024 16:46:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6A2896B0085; Mon, 18 Nov 2024 16:46:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56A676B0088; Mon, 18 Nov 2024 16:46:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 3988A6B0083 for ; Mon, 18 Nov 2024 16:46:58 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C09E0AD401 for ; Mon, 18 Nov 2024 21:46:57 +0000 (UTC) X-FDA: 82800549804.30.27DAC84 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf20.hostedemail.com (Postfix) with ESMTP id 3BA091C000C for ; Mon, 18 Nov 2024 21:45:54 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LXaJp09M; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3zrU7ZwsKCGkRHSLZOZPUNONVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--kaleshsingh.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3zrU7ZwsKCGkRHSLZOZPUNONVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--kaleshsingh.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731966350; a=rsa-sha256; cv=none; b=tNC9DIyUo0LLGcdYUITV1OgtuyHyyxVrdup+i9oPIAJNQY32fvxpG8jfKqa687oBCq6KLf fvJUQLdI3QR/caCcltdHBcuZzFLqXzPnrdBjFQKa19uuzQImcJSRaRhtmXo6ILjlWW/NRc 4tR6hQRe57gP55ZKRA852qKtjLzng/U= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LXaJp09M; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3zrU7ZwsKCGkRHSLZOZPUNONVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--kaleshsingh.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3zrU7ZwsKCGkRHSLZOZPUNONVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--kaleshsingh.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731966350; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=TvOCdV4s2TNa5ISzo3BdsjjuMEaKM722wwhwoXrHkjQ=; b=g672NIdge6aFKOjRDZ9eD6ZtY+w+Vu3WntoMCP6eTu9JnMJigrVZwys4tK1Cf5PlZclQ9D /Lbi0fs5YjYz/0tXxnlKd9h8nRVQY2jjVk0nK9LR9c5wcgk3TVCTTYeY3X1+j7ovcsuUuD 3WP+8bci8D0i2GM7Isj42R8DceqnpgU= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ee813065afso33079207b3.0 for ; Mon, 18 Nov 2024 13:46:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731966415; x=1732571215; darn=kvack.org; h=cc:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=TvOCdV4s2TNa5ISzo3BdsjjuMEaKM722wwhwoXrHkjQ=; b=LXaJp09M/MHjgRTh0fW95CiGegBBNCM0kwPQJpKkJFDp9hxoMrxvWOqj39Z4u5HWYU 82H074/ygCfy/UaUMUaC4pRYEWHc/wevtu+TdWxNaDzZTdmSlY7v75fzVyQsbPVIClBY cXlr3qGgdiqAZJ+ktJsxPG3BLGwrWyBP7bmKXxvwqgpVinfZDbP6+L1owX23ITGFzUBm XIX4XgfUA9/aDu7A2w34f0oExwq4Vq4SbWCGg9gtMk7ML+Tzgw8/BdtBQNaL4TRZzWTy Gc/tohEjvAG8PAzBnslKkTwuDH/AL9lWVEzpmK0WGKBIZi9BO0M0mlw3DpaxypOuKnVp pobw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731966415; x=1732571215; h=cc:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=TvOCdV4s2TNa5ISzo3BdsjjuMEaKM722wwhwoXrHkjQ=; b=vI+We7hVg8Voq5rWOeDbrEOFsn7VKsC67yHtlfOTAbhry3E3p8w7aQDL6VtrMFqsoq 5xI5bahcq7zuU7vHRId/QbEl1MtQfanXoO1BUEIrPhaNoY51/TAxgInsHZrei52mI2KA Os4UEqEaPGvIfSVNlgFpsnhS4VUBVDiGBhXxTQj0Pj0cr6eIl+PnaWK0GGhI3GoLXkU2 DF6gQXxJVtdoV6MujwmZw+7ytjPeoreQHleErJ5UdwVVDVcvPjbbmGnb8jEseIVw+yD8 lQ9VgWfIUmL/k6RZBiGDk+vYlI6fppkP9fHoW3u5Tuxv5xW7CR4wK3TaRQX5qfykthwk wAFg== X-Forwarded-Encrypted: i=1; AJvYcCWSnzqTLZf8zkzIEdbmn9AOXqpr9QsZdDRgGBlaI7X4fhWG35qyEaaFeYdznER5zNYbHh0XcgDS6w==@kvack.org X-Gm-Message-State: AOJu0YzKJijlQHnSxYEcHJe96c02s9pHRvCcgaCYJH6bPoXSgc87b76h V2C0ZsOhDRwhKgdAROCUGs9B/2ygXkrOQs6qjIYuT4Qk+UO7eGil9KMTo783XG536XHTPFPmYCD lRUl2+D8+skxR4mcmukK7Ew== X-Google-Smtp-Source: AGHT+IEuDWbTMXz+ht1hQLEvCDYEG9ppylXQkzguGm3T6mmWzkk/64xr+RI0ZQ0tDGlSNMX+q8PbO7BDoMupncte1g== X-Received: from kalesh.mtv.corp.google.com ([2a00:79e0:2e3f:8:ed74:6cb:48a0:1f9b]) (user=kaleshsingh job=sendgmr) by 2002:a05:690c:8bc1:b0:6ee:5091:4254 with SMTP id 00721157ae682-6ee55cf1b83mr921467b3.6.1731966414927; Mon, 18 Nov 2024 13:46:54 -0800 (PST) Date: Mon, 18 Nov 2024 13:46:48 -0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241118214650.3667577-1-kaleshsingh@google.com> Subject: [PATCH v2] mm: Respect mmap hint address when aligning for THP From: Kalesh Singh Cc: kernel-team@android.com, android-mm@google.com, Kalesh Singh , Andrew Morton , Vlastimil Babka , Yang Shi , Rik van Riel , Ryan Roberts , Suren Baghdasaryan , Minchan Kim , Hans Boehm , Lokesh Gidra , stable@vger.kernel.org, "Liam R. Howlett" , Lorenzo Stoakes , Jann Horn , Yang Shi , linux-mm@kvack.org, linux-kernel@vger.kernel.org X-Rspam-User: X-Rspamd-Queue-Id: 3BA091C000C X-Rspamd-Server: rspam01 X-Stat-Signature: iz73oxanw5r5jj1b4td68hh8qqghoi3c X-HE-Tag: 1731966354-64873 X-HE-Meta: U2FsdGVkX18Ruh3yg3tKto9pEiHr4fWDV33W9l4Dc/DARIgm2MFmOOYDEKOp3qj181UWqyBker1tudTuxCR7dpPwOWyV5+E2O8b5g3crBVyJBoheHh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A2yCfjDs5NlOBVfLRUgC40XPxfAciEBPb3dokulnExfwbwx+JEZHgQodt1IoVykkANUklICqPqf+CbTI/d3bnofUsuAzVGKkV02ayq8aeRs54nO9yv22Hf+uAjII5q+qlC+d32hx467ySm2If6mnUBQO9C0T1P9rKruShgyV376gEdzY0q/gpldUBHd40D574Q+eUHVmas+JAsgbiZaMoqrAmuTOV0fH+mzQCFAjJOVBjjdzFx7/VY3814dqEayvuU3wawnsFV7Of22HoDCgZVQp0RjeDe8ogWIy4XhHUdx30VTN0GU/F8mC3N5HnZbtQoqTXORgDkoDwdkpqoArQCsvdoYIhmBcqMWfW8R85E1eRBseOBBAhzmX7+FgXWzhCrkYV1ygiGumYVJ10wbYAk32m+OJg7KiUhx3bUJozITcZA7hOahgDI4iG0J7lzT+jiToRLNHmOulvIDf1JBEvh+7pPVzeb4xqnmEA4mEPRZAY8HQsn6PUWTF68ZQpaD3bO3qImClLCIGWKDsNq6dKp/d+ogq3NYe8kqhtKKjSKwlXSjB/rQKPPN/nqVGNHgivmmz33l88awCvQi44/yHLLnSYEJQxcXdIAXAjGvUenXJiB++x/Pl9g/TFT1nANAaoFee1JHXs/K5TlCdWPpjwvCrDfvJO0TwZE6fTxQGi/C6RbG+f1+jHSth4DD7iUr/zK04gV6I= 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: Commit efa7df3e3bb5 ("mm: align larger anonymous mappings on THP boundaries") updated __get_unmapped_area() to align the start address for the VMA to a PMD boundary if CONFIG_TRANSPARENT_HUGEPAGE=y. It does this by effectively looking up a region that is of size, request_size + PMD_SIZE, and aligning up the start to a PMD boundary. Commit 4ef9ad19e176 ("mm: huge_memory: don't force huge page alignment on 32 bit") opted out of this for 32bit due to regressions in mmap base randomization. Commit d4148aeab412 ("mm, mmap: limit THP alignment of anonymous mappings to PMD-aligned sizes") restricted this to only mmap sizes that are multiples of the PMD_SIZE due to reported regressions in some performance benchmarks -- which seemed mostly due to the reduced spatial locality of related mappings due to the forced PMD-alignment. Another unintended side effect has emerged: When a user specifies an mmap hint address, the THP alignment logic modifies the behavior, potentially ignoring the hint even if a sufficiently large gap exists at the requested hint location. Example Scenario: Consider the following simplified virtual address (VA) space: ... 0x200000-0x400000 --- VMA A 0x400000-0x600000 --- Hole 0x600000-0x800000 --- VMA B ... A call to mmap() with hint=0x400000 and len=0x200000 behaves differently: - Before THP alignment: The requested region (size 0x200000) fits into the gap at 0x400000, so the hint is respected. - After alignment: The logic searches for a region of size 0x400000 (len + PMD_SIZE) starting at 0x400000. This search fails due to the mapping at 0x600000 (VMA B), and the hint is ignored, falling back to arch_get_unmapped_area[_topdown](). In general the hint is effectively ignored, if there is any existing mapping in the below range: [mmap_hint + mmap_size, mmap_hint + mmap_size + PMD_SIZE) This changes the semantics of mmap hint; from ""Respect the hint if a sufficiently large gap exists at the requested location" to "Respect the hint only if an additional PMD-sized gap exists beyond the requested size". This has performance implications for allocators that allocate their heap using mmap but try to keep it "as contiguous as possible" by using the end of the exisiting heap as the address hint. With the new behavior it's more likely to get a much less contiguous heap, adding extra fragmentation and performance overhead. To restore the expected behavior; don't use thp_get_unmapped_area_vmflags() when the user provided a hint address, for anonymous mappings. Note: As, Yang Shi, pointed out: the issue still remains for filesystems which are using thp_get_unmapped_area() for their get_unmapped_area() op. It is unclear what worklaods will regress for if we ignore THP alignment when the hint address is provided for such file backed mappings -- so this fix will be handled separately. Cc: Andrew Morton Cc: Vlastimil Babka Cc: Yang Shi Cc: Rik van Riel Cc: Ryan Roberts Cc: Suren Baghdasaryan Cc: Minchan Kim Cc: Hans Boehm Cc: Lokesh Gidra Cc: Fixes: efa7df3e3bb5 ("mm: align larger anonymous mappings on THP boundaries") Signed-off-by: Kalesh Singh Reviewed-by: Rik van Riel Reviewed-by: Vlastimil Babka --- Changes in v2: - Clarify the handling of file backed mappings, as highlighted by Yang - Collect Vlastimil's and Rik's Reviewed-by's mm/mmap.c | 1 + 1 file changed, 1 insertion(+) base-commit: 2d5404caa8c7bb5c4e0435f94b28834ae5456623 -- 2.47.0.338.g60cca15819-goog diff --git a/mm/mmap.c b/mm/mmap.c index 79d541f1502b..2f01f1a8e304 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -901,6 +901,7 @@ __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) + && !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,