From patchwork Mon Feb 26 19:09:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13572724 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 24AA8C48BF6 for ; Mon, 26 Feb 2024 19:10:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 38B994401B2; Mon, 26 Feb 2024 14:10:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 33B2644017F; 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 1B4234401B2; Mon, 26 Feb 2024 14:10:04 -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 0A35B44017F for ; Mon, 26 Feb 2024 14:10:04 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D934A1209BF for ; Mon, 26 Feb 2024 19:10:03 +0000 (UTC) X-FDA: 81834895086.20.E11B7A0 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by imf09.hostedemail.com (Postfix) with ESMTP id E3985140010 for ; Mon, 26 Feb 2024 19:10:01 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GI3FDB7A; spf=pass (imf09.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708974602; a=rsa-sha256; cv=none; b=w7CjrSsnyXFo0vCieQdN0zZ/49uePfQWVAnkDnLDKPlOBtoEaDY3EtmjOFfal5bF+9zf7J oFdBZJtoGzOSHAxxiuExlApyeN8o3YANuIhdhJowMMUeGlH8ueqByDyYakAF5UUMMDVlrn cJ3E+raIOJskLa7BEqxVlpIZDeQ9yBA= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GI3FDB7A; spf=pass (imf09.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=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=GJ3WV1MyMnvFy0laLvbLjyXhu5lENNpTuhfkEsO4ulk=; b=nQ6sp/644xU/V6SyqNaGcrFv1jst2cEiqLiX3Z9z47rtq8988lcRRHckfolBpEFTtIj+du kSjmtqP+UsAE0rQv6xaZBI8nLIl0orquHzZsO6V95ZDg3yhj1UKzRTVoOeu9U3Af7t59c+ PpEo2BUnANaab5xHdVnpPz9e/jQE4QE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708974602; x=1740510602; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=NmgdWGUKdUuPH56dpQmxlcj6RB+edyhe9JvhgeXosrc=; b=GI3FDB7A4L7loxuFlkw3GSYt4xkL1fDlK+Kp8LsnDxd/3VAjGa/JwjDD JcswTHKH6CBPkMGgNMflyRToWMgmlqR6fLqIdqA/EnVdWFe7I9x1kCNyx Rj/zlsXq7lffJ6FHPe1hLobNeky8C68U+abakvVjES3jjnIpKRttczKWE 1ZoFRKNyyy1j+D/lAUYOMf7HXn+/YwxRQRHmzKfGZ2HOmMof8+s3K76vl 9hZqU0DvPL4T8vp9GXPFECDe/GmPymQfwmzVZw9LxNMaSMPEhJUhsCLed +oexXymWJEwYZA84YFNIENy7TR1XB/0yRNt+Vxtakf8/cdz++ZSuZjyfs w==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="14721346" X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="14721346" 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:09:59 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="6911445" 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:58 -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 2/9] mm: Introduce arch_get_unmapped_area_vmflags() Date: Mon, 26 Feb 2024 11:09:44 -0800 Message-Id: <20240226190951.3240433-3-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-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: E3985140010 X-Stat-Signature: sumewo9uakbx5xwhyd91ty7a4x3gtret X-Rspam-User: X-HE-Tag: 1708974601-752103 X-HE-Meta: U2FsdGVkX1/hB13eCsXx00V3H8x8jeKytemyFH0LcZIMY0Cc7xoiqHALYxY5NqnY9WJ6mkvwUsZzqhbBAqMs8KF8dHd0lp7xhc3NblHDfJFx8pZEz47dnA9/3IuRebHUnfIxzFA6yb7lLzy1Qm35u3zybESJnMmJ7RlPnX0rqoKY5h3BAfc4cXGJ2u4DLmjJWj3XorctmYtyVxIHJiglpgsaD6pOgQ9JPbBAoCeGRU+yC1A0tz/btLOZk/6hKW5MENiRfjq0S4TSn/evd6AgeuVN1kSHVtBNKKzxLXM1e5jSLR1nLvBIuq8J9emLJr+29VNBM43GhEw68G8EnvxbfbkkyCLGsEJ6R8Q8c+Jpo6iF8b1Mf/mrCEa2+Y3hTYPhh37q5s1J9gMMklqXO4FtUXD/458wZ3udwp/hyjNEQ9P6bKePyu/ZIIMQwaCRhQVTjPty7/9Xb22Qobz04Kjfmzhh7KV8UoQJ5C1QZHyvrnzUH5alDOA9P35n25LvA2MQfvl7zVVLAcqJa/ecOROjSpsw2ezqXufxXnslw7iO+nMYQZu5ZEQfv9Fsi7QkOlscXHI2JuU5rJ4qg4NMflmww6gykvxM/8GNmYhKwroHSrTQcXehBCjYd3LXLwPOMpeB5qXIBlWSTJi2ro9dYgNVuz6MTU4UggpcZwLq/iHQI5lOHvFawm7fjUUfhfyT0/5lCGLQqm55Qyz57ZqWV9Ir85H6ThzelPgvvQm/61+MX2vstFisx3OYwxUcUgBo+fIB5gBKWJTza7S0mxcQDRx8G9jvz6Dtb+AlhgDbica3MBmmYErU+0hWDrIeeDKTNEvkUdW+PaUhHR41Q/JE5Qz6UyC/heduigweeemYV8Vf5gLpkPHspioSPRCcvgldYsbhRn3OU/75OeM= 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. In order to take the start gap into account, the maple tree search needs to know the size of start gap the new mapping will need. The call chain from do_mmap() to the actual maple tree search looks like this: do_mmap(size, vm_flags, map_flags, ..) mm/mmap.c:get_unmapped_area(size, map_flags, ...) arch_get_unmapped_area(size, map_flags, ...) vm_unmapped_area(struct vm_unmapped_area_info) One option would be to add another MAP_ flag to mean a one page start gap (as is for shadow stack), but this consumes a flag unnecessarily. Another option could be to simply increase the size passed in do_mmap() by the start gap size, and adjust after the fact, but this will interfere with the alignment requirements passed in struct vm_unmapped_area_info, and unknown to mmap.c. Instead, introduce variants of arch_get_unmapped_area/_topdown() that take vm_flags. In future changes, these variants can be used in mmap.c:get_unmapped_area() to allow the vm_flags to be passed through to vm_unmapped_area(), while preserving the normal arch_get_unmapped_area/_topdown() for the existing callers. Signed-off-by: Rick Edgecombe --- include/linux/sched/mm.h | 17 +++++++++++++++++ mm/mmap.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index cde946e926d8..7b44441865c5 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -191,6 +191,23 @@ unsigned long mm_get_unmapped_area(struct mm_struct *mm, struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags); +extern unsigned long +arch_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, + unsigned long flags, vm_flags_t vm_flags); +extern unsigned long +arch_get_unmapped_area_topdown_vmflags(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, + unsigned long flags, vm_flags_t); + +unsigned long mm_get_unmapped_area_vmflags(struct mm_struct *mm, + struct file *filp, + unsigned long addr, + unsigned long len, + unsigned long pgoff, + unsigned long flags, + vm_flags_t vm_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 b61bc515c729..2021bc040e81 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1802,6 +1802,34 @@ arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr, } #endif +#ifndef HAVE_ARCH_UNMAPPED_AREA_VMFLAGS +extern unsigned long +arch_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 arch_get_unmapped_area(filp, addr, len, pgoff, flags); +} + +extern unsigned long +arch_get_unmapped_area_topdown_vmflags(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, + unsigned long flags, vm_flags_t vm_flags) +{ + return arch_get_unmapped_area_topdown(filp, addr, len, pgoff, flags); +} +#endif + +unsigned long mm_get_unmapped_area_vmflags(struct mm_struct *mm, struct file *filp, + unsigned long addr, unsigned long len, + unsigned long pgoff, unsigned long flags, + vm_flags_t vm_flags) +{ + if (test_bit(MMF_TOPDOWN, &mm->flags)) + return arch_get_unmapped_area_topdown_vmflags(filp, addr, len, pgoff, + flags, vm_flags); + return arch_get_unmapped_area_vmflags(filp, addr, len, pgoff, flags, vm_flags); +} + unsigned long get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)