From patchwork Tue Mar 26 02:16:45 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: 13603258 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 3496DC54E58 for ; Tue, 26 Mar 2024 02:17:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5DEFE6B0083; Mon, 25 Mar 2024 22:17:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 53B026B0087; Mon, 25 Mar 2024 22:17:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3DBE96B0089; Mon, 25 Mar 2024 22:17:21 -0400 (EDT) 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 123956B0083 for ; Mon, 25 Mar 2024 22:17:21 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id CC33C8028C for ; Tue, 26 Mar 2024 02:17:20 +0000 (UTC) X-FDA: 81937578240.26.51B323E Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) by imf11.hostedemail.com (Postfix) with ESMTP id F168040004 for ; Tue, 26 Mar 2024 02:17:18 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Bd3I7aQz; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf11.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.16 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=1711419439; 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=5ic3mPs7s2z6mq/2rkKIDBh9XLDk3GzkNtuRlzq0a2w=; b=xcQ1cFEo/4nJFLRenLhSazUVc4BGcB3yoIN5LMmarzzf027AUqhC8FdU9hIjU+vnJ0n5r2 S/7lP7KMffO3NzvbsVlqulV0w/2tduJ7ks8kHWhWFTU2XxTbdY4RhhGGHUTE0ixHBDq/8A vNqbJUB0VdkJGQm3uQFtRhlJOI61dKI= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Bd3I7aQz; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf11.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.16 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711419439; a=rsa-sha256; cv=none; b=rrBMzRipiT2tVaXcR323L8J1xPg9FqWmBZam8La+Lc3QM1EEO5s3GaOuEnLjRXl3TayNZy irpeDRayhIMoaHw6L9pUBbgkfe7tDW1re9xKx4PVkvog3Gy5kgk9gt25mRt8i15UPS/zN3 SX2B7RgPsMc1z6iGJ41Yq4+9KK/r8qs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419439; x=1742955439; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=y5DokqN/WlvuTFZmqP1zrzAjZ8lFdRvaKiOQWgxpins=; b=Bd3I7aQzZh9S83OqJA+ni5vSvIExnKVGMLgL4Ui7CDtK2uQ9z54XmzwE 1KIl0S4w8cX+Jk0pNxho3LjaNBWIOeGNZGaM1IMh4wRUvD1K6aQg7Md5G JM4A/bYyDkzn8mOCEeaMYYPJuT7LMG9z5Gh48laZNQqlSneu3SBXH7Mf/ 6TMPla0pKTLy1Jn5RSdk9NDudIenEeU054WHKOL2koJ7NNk8VkvgBm9UN u09wnTrbowYv3TdVghEZucR2ozxWXYVVhkpHym3AmQW61NTy1iZeQDCuM 2ufGnQgzm/H5PYpuTFzpPiNfrPM40b7G58h4N5fFP9rxUUEnIzZvQFLS4 A==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564239" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564239" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489872" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:14 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 03/14] mm: Introduce arch_get_unmapped_area_vmflags() Date: Mon, 25 Mar 2024 19:16:45 -0700 Message-Id: <20240326021656.202649-4-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: F168040004 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: wq4auogxnbdpst6mgop1yiwpxx59uaz7 X-HE-Tag: 1711419438-565859 X-HE-Meta: U2FsdGVkX18stLfT+Dg1njNDZtn6Cmjaq+UorJQBfz2RDdUHBqh9KVFk8xR7aCw53JuXGfXs1jlguiwyv3KAcwXQDjvw8UDqMXxWOk9covkXg1HvfMs87t6XdlM0L1spXr0qQqsXYCPHw4Q/MyCVymqbhVZCPBSsqcC2DeRIy/bAMo42BPacDmNQMl4k2TabwFUUnh7x19FIJX8A40C28K2a9YsQHyxZ2214Iq7PsPMQ1jlteJ1KADCYI1Nm1HNfIZgScqbvmM0Wtvl/gPPFi2L0oVhdv4upWs4aPjWuJKXN70k20xlslKh8YfR+9qE+0KCQwrEQjpNRBzCQQHt+zz7wJXudacIcdoDCFl1y1I5jGeGub6+uu6x5pXz124+SKnnsmECBVfYzgLPf/zCYjojRE9eSx8ZeXIIZ4b9QlFUlySHzBG07VrsFB5ntM/SpH6w5TgHUWPe1BEK8IeCnUj+7CQqLjN8b/lJUda6MB0jeuHwU1dc/iEwtfiJfDaAyN2H2SELsia+yQsZTVzq+HJJhTANSvtWLswVCwSOGHih2fZUxT3iTe/XgxBwIaaPlSkGhXUDzd/9z4QL1XYbqjxylMvJ3We++5WM1HWkScPGZ1L5gf2i9Q7fC8kwIexqjY/Tfz3t5Bp40bID0QVTxHfPLS8CgtmRpnPCrRiruN+LT9YQJTgHaIHacc1DYxurAEr4zLnbHR477potZwFW8I10ulY1svFAFS3QVPXrcKdAuEiC4bpGqvLyGls9PjztOvvCqFp8MFJv5QEoZd+ZmMxHSC29r5zDZowuj1BWyBNgUC/WrD3x0UdbrBGnUOrBhPMnunzUuVF02PA9yve7Kpnz89KmalL8WgJutn22/GahmMyIQkSRUQ0D6+Eau1bXc0QKjdJmnPtQ= 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 --- v4: - Remove externs (Christophe Leroy) --- 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 ed1caa26c8be..91546493c43d 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); +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); +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 224e9ce1e2fd..2bd7580b8f0b 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1808,6 +1808,34 @@ arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr, } #endif +#ifndef HAVE_ARCH_UNMAPPED_AREA_VMFLAGS +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); +} + +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)