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 37168C54E4A for ; Mon, 26 Feb 2024 19:10:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A78C4401B6; Mon, 26 Feb 2024 14:10:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 907A144017F; Mon, 26 Feb 2024 14:10:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C69F4401B6; Mon, 26 Feb 2024 14:10:06 -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 3D74944017F for ; Mon, 26 Feb 2024 14:10:06 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2300B1C0B06 for ; Mon, 26 Feb 2024 19:10:06 +0000 (UTC) X-FDA: 81834895212.01.7C1B4CB Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by imf07.hostedemail.com (Postfix) with ESMTP id 33BB040008 for ; Mon, 26 Feb 2024 19:10:04 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=F2rNtU6k; spf=pass (imf07.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=1708974604; 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=xPLva11Xq7XC8tYhVSCbr9K0IrR4H1dbybdarN6i4iY=; b=V904t29W3xv4gy5+Ynt2/KYPb6Y8/5tSax0EEnjxpc93HgNW/KMhuFJbDA3sugn2mrRdCJ aqH1HcDbWclS0eI6UurkqeIFi1YftAIOdsa8EV5Wu+TVEd8b/AVLkyMr7vVjy9lMCaI9Zy PZQcbVIb0yTYsOEFb+khvxG/3LAe2GI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708974604; a=rsa-sha256; cv=none; b=EeM12yZlR5wzR5BN8o4lFo3dsWcrOgkonR00C5wwxA/dvrR21B9klMxlfVe2sQmARflQaP yHxkZm5OCH160t4bI/p/lEqtiumEkF9lsN6cc8JkYbN8lUfB8KMbEmAxy06/35vphZIsys Zctmz55Jd4kp0xJqunRvOrfzifkQbTA= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=F2rNtU6k; spf=pass (imf07.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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708974605; x=1740510605; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=U3F9hHxdfZ7A6mCry7GoiZtnuJCP/wXQbUGWHcLnyx0=; b=F2rNtU6kPY4lJwDGnQB/SsdN8Gxd5kYU1/fkSo89vVfqMzZ+1M9ybOa0 MWsGZYme5+7FHmr5KPFqoTE5pmC3x5fvHJqc2rI/ln8jQtXPbKMokY7Zq El+6qYn8qB9L7rKkcmfoafifzRkANs5Tw5gHsPIWI16SejSFm/JNbFmfY Q5r788LuLDZ28tWLDccsLDHSUR/iq/a+JGKnZM+JL+8y0aAQmKtSry5So XId+nP5bxBlGmqvJeUtlosfqul+ynOOE3SRgCU1Os+Hv17TEnxyydHkAy QF9DehaJyeAI+6Y9CBjcmw83AV43X+WRDt3rzpKF/OGZavN7GaxudIocT g==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="14721404" X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="14721404" 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:03 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="6911463" 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:10:02 -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 6/9] mm: Take placement mappings gap into account Date: Mon, 26 Feb 2024 11:09:48 -0800 Message-Id: <20240226190951.3240433-7-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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 33BB040008 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: hwm14qaxpqz4aoi5k7zx11ankczt4wms X-HE-Tag: 1708974604-665373 X-HE-Meta: U2FsdGVkX186etTdsjS02tbynFChWESa/Li7oJ56YlHW4M9xbf7nFV2ircZ3iIASV8g8uU2gtxQWhNKkSWRNxTmnn1JzAIpM0aLOg1up8yiCrSNChicKkJ1aEEMJYIHQP1gxTwP1AFs33h2ol4LQ1u3NvAoesz3j2At7YhL/m5QzLH1glSUTRQYUtDakIScLSry6BAo8/w6t/wpCkUsPuITRTcrbrjyirth//qTXLABemSkJeD9nMXB9LI5AtZmA+gWDRL90MXcw+qTqCZJWLtrKSOg9buVl9iieJPLxeu2ypBgrVYoCJ2hD+feZyMr87z5ynL+C2LSyM2Ad0Xd9aBNLfn7TJtGsdpKdcKlau3SvE/XPI7hUaUqRmYS2KJ/JOXqv0DBN+KkJDEFw63Xy16RR9CMoA03ZaAvcD3k2SyoNpyQXuz1CI+01xoOaoqO7YWqJueHHUiY445wGGbxf71U5dDPQePay99nBTP0jpTsBrx28jyIN5j3MVXK4+CDK0/xLzq9e7HdmvAKtrdzIIquxXfiD3WpMrIg44xT3PmkuWksjJybg8fCkaQyAZno+lBkTBrHJhV8VLm2GCLhSlBB8tXDymHoZtdTUf/lj+MaXe55qub2GWN+uxPaESY91o5viCR5LCKWhFlX4Vj5HqgnwapiT8+PQuUQv66n9aic18sqc6xpq9y4h5Ati78z8YgU9jsMdzEU3X6nkmiCIVmjDO2CWAIdK9iPtyVSALrPU5eVIK0VJDYzJ4/N2f/fxQd+kb4PMiYGK6NvzgMwWE6nG7x16hInpqwjACWZidlsjwbSUq02Tc8MIYf097LI4/qwdjJOkqzQbAi+7WKg+QmFKy9J+PsaH 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 --- 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 a091181ef65a..19fc1eb86b9a 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3406,6 +3406,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 33af683a643f..3d7642eb84ea 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1574,7 +1574,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; @@ -1586,7 +1586,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 + * fufill the start gap, the next steps is the minimum to align + * that. It is the minimum needed to fufill 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 */ @@ -1625,7 +1631,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