From mboxrd@z Thu Jan 1 00:00:00 1970 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 D95A2C54E58 for ; Tue, 12 Mar 2024 22:29:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4866B8E0027; Tue, 12 Mar 2024 18:29:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 43A5B8E0011; Tue, 12 Mar 2024 18:29:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 28C038E0027; Tue, 12 Mar 2024 18:29:13 -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 148598E0011 for ; Tue, 12 Mar 2024 18:29:13 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id D4F02C05F4 for ; Tue, 12 Mar 2024 22:29:12 +0000 (UTC) X-FDA: 81889828944.27.FFCDEF5 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) by imf11.hostedemail.com (Postfix) with ESMTP id DA3064000C for ; Tue, 12 Mar 2024 22:29:10 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=hR9MwyJt; spf=pass (imf11.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.198.163.15 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=1710282551; 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=nmVVEEIA3J5OAciiBC3omuJ+xpJDNNQ4gqbzwaKVhfs=; b=Rk1YCK60c1CWq6tTisxqijpfrYKnzCOkZGXTdceLo+q2FCNjOMbaLCAFwG0fgAdImyHT3o y7bjR95TAjveOKdGjlDy6B6pkYzvVpoV/etLAseEeKQn+g5olPbB5BjkzOTBd7YC+tk6cn hJILPmyXR46zLtq4l6UQthDvkgoGs2g= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710282551; a=rsa-sha256; cv=none; b=1VZ/hmpe/diP53XQeUPdMaKcpQi54HwHGr4GRfFDkaYTJBfmTnM0mXzf8iKVZO/Jqs0p6H 95qUsaQLdlbH2VoaHwmRahY7wMv7Gh5gUk8763qUFRQPiIOZl0FFAOy1KmAekha9XypJNU 3h/vDg1m0R0UdPLvfmJhklqIDwKQ/Wk= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=hR9MwyJt; spf=pass (imf11.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.198.163.15 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710282551; x=1741818551; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DqX79GXBAG2/j46/SZGgINNlJjEl0m3V0W63EM+RdAU=; b=hR9MwyJtamXgJ/7iuO8TvE7NOlD+PbAmtBr1hOkGy4RaFhUqFH0K1Nrg jv5iK6FTQvrhLWmBGGNjAl1qMvT43i8A8kbcDq5YlRZimKVlhmDWmz6q3 ndH6BB0UqQstD0uUj2RMpc51wQMZ8ryyVbPyXOnEi4wtdnDn5WDLdJF2S iRx714m17vE4m74qfiHERvpfpvT7HI4+soXzn/Ghla1k6zwlyS4eXGICk 8cxHlhm8YX7Bqz15zTknzERb1B12JBZr7Z6Ndjq/wjYJMjgOwzxkPzDZ1 8e3ZxWlFgeCHq4NT6SQzt1c6N4fBrJ2+FnaA+OQkvYJgaeclFIR4eRX+k w==; X-IronPort-AV: E=McAfee;i="6600,9927,11011"; a="5192041" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="5192041" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 15:29:04 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16356874" Received: from gargayus-mobl1.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.255.231.196]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 15:29:04 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, 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, christophe.leroy@csgroup.eu Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, rick.p.edgecombe@intel.com Subject: [PATCH v3 09/12] mm: Take placement mappings gap into account Date: Tue, 12 Mar 2024 15:28:40 -0700 Message-Id: <20240312222843.2505560-10-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240312222843.2505560-1-rick.p.edgecombe@intel.com> References: <20240312222843.2505560-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: DA3064000C X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 769ueffdspjnzu88uhpmc4i3bcedzzqc X-HE-Tag: 1710282550-251367 X-HE-Meta: U2FsdGVkX1+/WJK0+5AuNpWR7CeZqaP6wbVe5T45orAQ5FpZoshEFCvQwqWRT+9TXWTF4S9aQPsexd+vw6hKVUF/Jje85FbaZ4k4Wwvdjaxi/K3HACeaja1q5YJyKdgTp0vV5qiFMfFucf642lLrErCs3E9Ng1p0NWUBsh9z7v5p6OMkvQh62FNyOQS8faEb5CiCmtbAqBCgN8kDJCugkJUmwWWTqNyIjn6ZOpNJvGMSHbUBxL/qb1Be0HqLzlWeS+ZXV2kEKxsxCe3newLKaMBLLFNJ/g0Gd7h4a4T5STgvrNDEcPjO7w2Pp3pj5pKC65N02IJCMuwMa6H+rYT8Zw8U158lyKfEr/ue2C/aYcwzMdiyXacOUyH4DxGf54VQirIy/ycZuiGxZQlJlpRmCMjZ81dpEg2JhFWbd/hpcD81mmnCn843oWd1uaSoEZUQfm7RNHP+1Xh31e3iJVBOhd5RqRTxawMlBALCKf6htp9AjvG2HLHw6qXIyGY+hlrauJJBvsutdmE9lQsH3khb1FkyxJbeALfqikakvBofAep+TaghJmnu4X57asCbzFLgIki2ccHvkEkY7hpqn5sfwTL1Rd55i4XWv7f+FwOxJSlISrH6GQ9tJ7MkBNZtTBx0efAfycWEQArxOqUT6mgqO8xfRXbAljMj9oIdDTDKmfAZthJIb73aJmNYisTu5MQcLyrYhkK3xatJknS6e642U80AL+V8PFu5W1VEkw5D+CE2TaG+BNoDlNlYCwZXO1asCL9AV5mgGZB+Htk5njabg1Za2G58HdhxBAQd+1TPiKiX79eIiPYDRgxQ3iFqWb31DjLH5hY2Sr2xhSP+mVNSI/aTlsi69vgI 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. For MAP_GROWSDOWN/VM_GROWSDOWN and MAP_GROWSUP/VM_GROWSUP this has not been a problem in practice because applications place these kinds of mappings very early, when there is not many mappings to find a space between. But for shadow stacks, they may be placed throughout the lifetime of the application. Use the start_gap field to find a space that includes the guard gap for the new mapping. Take care to not interfere with the alignment. Signed-off-by: Rick Edgecombe --- v3: - Spelling fix in comment v2: - Remove VM_UNMAPPED_START_GAP_SET and have struct vm_unmapped_area_info initialized with zeros (in another patch). (Kirill) - Drop unrelated space change (Kirill) - Add comment around interactions of alignment and start gap step (Kirill) --- include/linux/mm.h | 1 + mm/mmap.c | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index d91cde79aaee..deade7be00d0 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3418,6 +3418,7 @@ struct vm_unmapped_area_info { unsigned long high_limit; unsigned long align_mask; unsigned long align_offset; + unsigned long start_gap; }; extern unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info); diff --git a/mm/mmap.c b/mm/mmap.c index b889c79d11bd..634e706fd97e 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1582,7 +1582,7 @@ static unsigned long unmapped_area(struct vm_unmapped_area_info *info) MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); /* Adjust search length to account for worst case alignment overhead */ - length = info->length + info->align_mask; + length = info->length + info->align_mask + info->start_gap; if (length < info->length) return -ENOMEM; @@ -1594,7 +1594,13 @@ static unsigned long unmapped_area(struct vm_unmapped_area_info *info) if (mas_empty_area(&mas, low_limit, high_limit - 1, length)) return -ENOMEM; - gap = mas.index; + /* + * Adjust for the gap first so it doesn't interfere with the + * later alignment. The first step is the minimum needed to + * fulill the start gap, the next steps is the minimum to align + * that. It is the minimum needed to fulill both. + */ + gap = mas.index + info->start_gap; gap += (info->align_offset - gap) & info->align_mask; tmp = mas_next(&mas, ULONG_MAX); if (tmp && (tmp->vm_flags & VM_STARTGAP_FLAGS)) { /* Avoid prev check if possible */ @@ -1633,7 +1639,7 @@ static unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); /* Adjust search length to account for worst case alignment overhead */ - length = info->length + info->align_mask; + length = info->length + info->align_mask + info->start_gap; if (length < info->length) return -ENOMEM; -- 2.34.1