From patchwork Mon Feb 26 19:09:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13572726 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 2EE29C48BF6 for ; Mon, 26 Feb 2024 19:10:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 032394401B4; Mon, 26 Feb 2024 14:10:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EFA6A44017F; Mon, 26 Feb 2024 14:10:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D77664401B4; Mon, 26 Feb 2024 14:10:04 -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 C7D2B44017F for ; Mon, 26 Feb 2024 14:10:04 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 9451A140970 for ; Mon, 26 Feb 2024 19:10:04 +0000 (UTC) X-FDA: 81834895128.15.B0B6BB6 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by imf16.hostedemail.com (Postfix) with ESMTP id A35C018000D for ; Mon, 26 Feb 2024 19:10:02 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cBElWRVi; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708974602; 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=w9uxx0p4IqyzsB+rWsNbj/igvIL8fkPgh5yQf8P8jHg=; b=HSAFOCz+AowEY2BmE7cwKtEzZurDznzTS1cn4HQcpMxZXM3HG8s45FG+cHHEvhDMitf3pf X7mUzYWjdBpZwvYhKYKxI9esxsZKoF6O1lk+s7p3RMmsZE/8gpKuu6CUIe3NPMs293yzq8 e8M9m1PHCXKJD1X26IaKGtjZY8sQqBw= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cBElWRVi; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708974602; a=rsa-sha256; cv=none; b=cI8FA5TEaDsxBG8jnl1eI0vGfED3a57uaR4eC+ABMlEHF5XyO3ENP0Y3fQqcYgc6HAnlyk FmgGNgEPhh1/J68dKbPEVWmMyNwnSaLWEP3T6OmZ1dEdgEN4EDDgboAAOfEl7dz5bicrSX rOC6tM1UQkFgFtzAiRDyQsJY26A2+No= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708974603; x=1740510603; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=P5XlQHAG5P20JDtEHex7uiurCNEBN1oum2HZdu24mXg=; b=cBElWRVinfatkFy7iksgb9z1+I/4cCNbPIOke0pDkZvucoFbc0ss+czX SoQsaiF0gikkekP13zZYy9V/MtiXRgKYE7KeOjZ5GgLqoqv4eTUwZZpna gEIDAI2ql58lFHYScsvxLmsoalJwduoFPh/5cS2pjF5UJWVtMa+C5Wbkc e8Oqummu9Hd7jMzFWE+pNEsY7NtlQxVterqoH+EMT5sKIyKesx0uomKeI W/cBBIBr6cO64x/9t79rlG6zauAIYQgDtW9NT214XaRqgT3GRtJuCHryQ PTcK6nXJRJ814sd+AveoF5xLM/02pZghGHquUKKRkHXigSzo7BvvhbLCZ w==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="14721375" X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="14721375" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:10:01 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="6911452" Received: from bdmirand-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.3.213]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:09:59 -0800 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, debug@rivosinc.com, broonie@kernel.org, kirill.shutemov@linux.intel.com, keescook@chromium.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, luto@kernel.org, peterz@infradead.org, hpa@zytor.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH v2 4/9] thp: Add thp_get_unmapped_area_vmflags() Date: Mon, 26 Feb 2024 11:09:46 -0800 Message-Id: <20240226190951.3240433-5-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> References: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: fzpp8g4ksn3qz71cd5pi5j4rzup6rg84 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A35C018000D X-HE-Tag: 1708974602-409473 X-HE-Meta: U2FsdGVkX19GzYMdfIGUylllaW+uIH/Vfjucw7hM/hB8Vnzm0JA/1M5lan+KNrAlJuUYw8ALP4kDO/grYkoGFjviA+DHcFufGJCd3OoSXb1eUbFaUVBHFcwydn9vw2q2SYkozMG/csGDtmBc+5emN3BFkZe66uvV+knqzipevvhU9kPdlHcN0WcyOLXpne0IlO7oAcdrI0yaz3ZPCJLDL2us5NodvGWF2u0zZVzXJBFn3NyPbPUR7MTMUAgAR59hV8qa8iGgPQqEo8g/V4aUMoLxJrP9BMTBQEIeVI97kLvUaZ1cq1G4wuOqh2AAeea2q/W1a8Jno8giV5TRZTM7QYXFomert77NuZ7wQT7YOm+PPinJnwXu+16ITaTDRieWORCCLGPwoJ59TqMmnS5jTgpcYsiAg5XzsZmW6NCMQ7KD505R7EbGpAT+Q9rS+tlf8o5dLMn+7xwKJzwbvedTG60rd8WC2TPWO8SpRTInkX3B5bJQrsFS7WnRli3EQF1EGFdebJXfvHEf5UKNYs66DKb0ssylaFJtRPAUT7ee/uSYt3lT1L55FdcH4C9GHC7hh9wGxt5VQ2JtPvFh4PYY5Pw+I1sJ526RgRCQXXv2O6g72+vgNqZprn6rLN3kmzMmuM/oDelyMDD82cKdcvWgr0WO3LB4COM8mg3wXxE4WC/wKAQY1GSG/qKUqZg6+Uv0aqzS1an9FJuiv87i0A3R09NJKDJCzEY5H/PElThXfN2KI3xiInbwjhIPrxkj2rw1eK0MQbHgeu3WaVTYY9nFPNweE9t/3dbeFizvC6F+5p2X4sWnWU286nbYOoRj2vTcD9KapgrxOpaqwhAuavew+YCxUZuPT6F20Mv905xDIa2A+YBpcc/s3nahXe8k5Kv+mCwf2itKoYg= 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: When memory is being placed, mmap() will take care to respect the guard gaps of certain types of memory (VM_SHADOWSTACK, VM_GROWSUP and VM_GROWSDOWN). In order to ensure guard gaps between mappings, mmap() needs to consider two things: 1. That the new mapping isn’t placed in an any existing mappings guard gaps. 2. That the new mapping isn’t placed such that any existing mappings are not in *its* guard gaps. The long standing behavior of mmap() is to ensure 1, but not take any care around 2. So for example, if there is a PAGE_SIZE free area, and a mmap() with a PAGE_SIZE size, and a type that has a guard gap is being placed, mmap() may place the shadow stack in the PAGE_SIZE free area. Then the mapping that is supposed to have a guard gap will not have a gap to the adjacent VMA. Add a THP implementations of the vm_flags variant of get_unmapped_area(). Future changes will call this from mmap.c in the do_mmap() path to allow shadow stacks to be placed with consideration taken for the start guard gap. Shadow stack memory is always private and anonymous and so special guard gap logic is not needed in a lot of caseis, but it can be mapped by THP, so needs to be handled. Signed-off-by: Rick Edgecombe --- include/linux/huge_mm.h | 11 +++++++++++ mm/huge_memory.c | 23 ++++++++++++++++------- mm/mmap.c | 12 +++++++----- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index fa0350b0812a..ef7251dfd9f9 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -139,6 +139,9 @@ bool hugepage_vma_check(struct vm_area_struct *vma, unsigned long vm_flags, unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags); +unsigned long thp_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, unsigned long flags, + vm_flags_t vm_flags); void folio_prep_large_rmappable(struct folio *folio); bool can_split_folio(struct folio *folio, int *pextra_pins); @@ -286,6 +289,14 @@ static inline void folio_prep_large_rmappable(struct folio *folio) {} #define thp_get_unmapped_area NULL +static inline unsigned long +thp_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, + unsigned long flags, vm_flags_t vm_flags) +{ + return 0; +} + static inline bool can_split_folio(struct folio *folio, int *pextra_pins) { diff --git a/mm/huge_memory.c b/mm/huge_memory.c index e9ef43a719a5..f235f6d3ff62 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -628,7 +628,8 @@ static inline bool is_transparent_hugepage(struct folio *folio) static unsigned long __thp_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, - loff_t off, unsigned long flags, unsigned long size) + loff_t off, unsigned long flags, unsigned long size, + vm_flags_t vm_flags) { loff_t off_end = off + len; loff_t off_align = round_up(off, size); @@ -641,8 +642,8 @@ static unsigned long __thp_get_unmapped_area(struct file *filp, if (len_pad < len || (off + len_pad) < off) return 0; - ret = mm_get_unmapped_area(current->mm, filp, addr, len_pad, - off >> PAGE_SHIFT, flags); + ret = mm_get_unmapped_area_vmflags(current->mm, filp, addr, len_pad, + off >> PAGE_SHIFT, flags, vm_flags); /* * The failure might be due to length padding. The caller will retry @@ -662,17 +663,25 @@ static unsigned long __thp_get_unmapped_area(struct file *filp, return ret; } -unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr, - unsigned long len, unsigned long pgoff, unsigned long flags) +unsigned long thp_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, unsigned long flags, + vm_flags_t vm_flags) { unsigned long ret; loff_t off = (loff_t)pgoff << PAGE_SHIFT; - ret = __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE); + ret = __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE, vm_flags); if (ret) return ret; - return mm_get_unmapped_area(current->mm, filp, addr, len, pgoff, flags); + return mm_get_unmapped_area_vmflags(current->mm, filp, addr, len, pgoff, flags, + vm_flags); +} + +unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, unsigned long flags) +{ + return thp_get_unmapped_area_vmflags(filp, addr, len, pgoff, flags, 0); } EXPORT_SYMBOL_GPL(thp_get_unmapped_area); diff --git a/mm/mmap.c b/mm/mmap.c index ac7601d05e89..e02bb17fef5b 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1857,16 +1857,18 @@ __get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, */ pgoff = 0; get_area = shmem_get_unmapped_area; - } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { - /* Ensures that larger anonymous mappings are THP aligned. */ - get_area = thp_get_unmapped_area; } - if (get_area) + if (get_area) { addr = get_area(file, addr, len, pgoff, flags); - else + } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { + /* Ensures that larger anonymous mappings are THP aligned. */ + addr = thp_get_unmapped_area_vmflags(file, addr, len, + pgoff, flags, vm_flags); + } else { addr = mm_get_unmapped_area_vmflags(current->mm, file, addr, len, pgoff, flags, vm_flags); + } if (IS_ERR_VALUE(addr)) return addr;