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 8F8E2C48BC4 for ; Fri, 16 Feb 2024 13:12:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0BBDF8D0006; Fri, 16 Feb 2024 08:12:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 044148D0002; Fri, 16 Feb 2024 08:12:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E4E898D0006; Fri, 16 Feb 2024 08:12:49 -0500 (EST) 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 D74B98D0002 for ; Fri, 16 Feb 2024 08:12:49 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 73DC0A02DE for ; Fri, 16 Feb 2024 13:12:49 +0000 (UTC) X-FDA: 81797706858.26.75E2893 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) by imf18.hostedemail.com (Postfix) with ESMTP id C65261C0007 for ; Fri, 16 Feb 2024 13:12:46 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=WNYM3YER; dmarc=pass (policy=none) header.from=intel.com; spf=none (imf18.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 198.175.65.16) smtp.mailfrom=kirill.shutemov@linux.intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708089167; 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=v38gRzCdvLIYU70HeI+9b6ps+Nmy63jNlk5vL0t5G4s=; b=OiTZiN010Jy82POiUAQeYrBxgHnFR1U7ZJEKjPT5HxZkbkOlbmahIEuJJiat4MSwbTu6XZ buvm942asUz/XzV80I6qMkrwbETd+yr6fjV1kQ1q2tGQ4ZcWppeXBovRMt6OEJnzCeFrPp RKxxHrgAEZohgCFV1yjBXcqs6Vbuf/I= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=WNYM3YER; dmarc=pass (policy=none) header.from=intel.com; spf=none (imf18.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 198.175.65.16) smtp.mailfrom=kirill.shutemov@linux.intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708089167; a=rsa-sha256; cv=none; b=FG9YRfhCM6V5SoK20UhgQtDJqZOnIrgWtfpfn/fFsP7g+ilaU62bCSnUr45zktnBU1t64e BWncntDMTCMtCrv1cqtgRS4GSf+LytBP5FYd9yf5z3uvqfaUc5OFXynsw97TztHu7IYN25 wHbTg2igufyOi1e+uLloa3xz6hyRPnE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708089167; x=1739625167; h=date:from:to:cc:subject:message-id:references: mime-version:content-transfer-encoding:in-reply-to; bh=j1gARnrStxs9TGrBtAtjswrLM8Ze75CW5vUX+LzHO+0=; b=WNYM3YERoiukKi7esuZBl7C8ktYM9G34k9Y/K8tEzRd1QQa6b1wm4Etb dOgG7ZXKP3y71l3Wnenrr4HUV0b8NDELZQAnz56N7nlCHEZ58pElkX0BM yNIpYbYbCGVjpdqZshExpkao48OFK6P6cbhwPDUWxa5xg1gW5wy9pDVbh LhOOZm+R9Oi1GZCjPxBPzuT47CIxJEONjWxeSt9zvji3SRTkcTOFDwvjx 9X7ZYaY8TlJIP9GMoa4R0WzBhcuoSZ7BQ2L3ZrefJdvFFa7ruR6wS9RXw LnhibCvnTikLchpbAqbMYUd5m1byUcpOoBIhrXokDw73/WrzznUJggJa4 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10985"; a="2359329" X-IronPort-AV: E=Sophos;i="6.06,164,1705392000"; d="scan'208";a="2359329" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Feb 2024 05:12:45 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10985"; a="935846694" X-IronPort-AV: E=Sophos;i="6.06,164,1705392000"; d="scan'208";a="935846694" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 16 Feb 2024 05:12:41 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id C37C42CB; Fri, 16 Feb 2024 15:12:39 +0200 (EET) Date: Fri, 16 Feb 2024 15:12:39 +0200 From: "Kirill A. Shutemov" To: Rick Edgecombe Cc: Liam.Howlett@oracle.com, akpm@linux-foundation.org, debug@rivosinc.com, broonie@kernel.org, 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 Subject: Re: [RFC PATCH 5/8] mm: Take placement mappings gap into account Message-ID: <3ynogxcgokc6i6xojbxzzwqectg472laes24u7jmtktlxcch5e@dfytra3ia3zc> References: <20240215231332.1556787-1-rick.p.edgecombe@intel.com> <20240215231332.1556787-6-rick.p.edgecombe@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20240215231332.1556787-6-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: C65261C0007 X-Rspam-User: X-Rspamd-Server: rspam04 X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z0ooZ1WyfP6goWBfpYqY4EU0sqnTp9nEMwl1ucxl7LUT6Pgl7QNrVvAgM7h2RrGUqhcfSD+qUGpuc3ewWyn27AT49Z1JoIMNmAkDlWp3jhzsLSSCS2JkwRMPFpPbZuHgY0TPgNcIBGeVHo= 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: On Thu, Feb 15, 2024 at 03:13:29PM -0800, Rick Edgecombe wrote: > 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. > > So define a VM_UNMAPPED_START_GAP_SET flag to specify that a start_gap > field has been set, as most vm_unmapped_area_info structs are not zeroed, > so the added field will often contain garbage. Use > VM_UNMAPPED_START_GAP_SET in unmapped_area/_topdown() 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 > --- > include/linux/mm.h | 2 ++ > mm/mmap.c | 21 ++++++++++++++------- > 2 files changed, 16 insertions(+), 7 deletions(-) > > diff --git a/include/linux/mm.h b/include/linux/mm.h > index 9addf16dbf18..160bb6db7a16 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -3393,12 +3393,14 @@ extern unsigned long __must_check vm_mmap(struct file *, unsigned long, > > struct vm_unmapped_area_info { > #define VM_UNMAPPED_AREA_TOPDOWN 1 > +#define VM_UNMAPPED_START_GAP_SET 2 The flag seems to be an workaround not to clear the structure. I think users need to be updated to clear the structure. In most cases rework code to use C99 struct initializer would do the trick. > unsigned long flags; > unsigned long length; > unsigned long low_limit; > 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 936d728ba1ca..1b6c333656f9 100644 > --- a/mm/mmap.c > +++ b/mm/mmap.c > @@ -1567,14 +1567,17 @@ static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) > */ > static unsigned long unmapped_area(struct vm_unmapped_area_info *info) > { > - unsigned long length, gap; > + unsigned long length, gap, start_gap = 0; > unsigned long low_limit, high_limit; > struct vm_area_struct *tmp; > > MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); > > + if (info->flags & VM_UNMAPPED_START_GAP_SET) > + start_gap = info->start_gap; > + > /* Adjust search length to account for worst case alignment overhead */ > - length = info->length + info->align_mask; > + length = info->length + info->align_mask + start_gap; > if (length < info->length) > return -ENOMEM; > > @@ -1586,7 +1589,7 @@ 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; > + gap = mas.index + start_gap; > gap += (info->align_offset - gap) & info->align_mask; Do we care to check if alignment itself would satisfy start_gap requirement? > tmp = mas_next(&mas, ULONG_MAX); > if (tmp && (tmp->vm_flags & VM_STARTGAP_FLAGS)) { /* Avoid prev check if possible */ > @@ -1619,13 +1622,17 @@ static unsigned long unmapped_area(struct vm_unmapped_area_info *info) > */ > static unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) > { > - unsigned long length, gap, gap_end; > + unsigned long length, gap, gap_end, start_gap = 0; > unsigned long low_limit, high_limit; > struct vm_area_struct *tmp; > > MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); > + > + if (info->flags & VM_UNMAPPED_START_GAP_SET) > + start_gap = info->start_gap; > + > /* Adjust search length to account for worst case alignment overhead */ > - length = info->length + info->align_mask; > + length = info->length + info->align_mask + start_gap; > if (length < info->length) > return -ENOMEM; > > @@ -1832,7 +1839,7 @@ unsigned long mm_get_unmapped_area_vmflags(struct mm_struct *mm, struct file *fi > > unsigned long > __get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, > - unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags) > + unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags) Unrelated space change. > { > unsigned long (*get_area)(struct file *, unsigned long, > unsigned long, unsigned long, unsigned long) > @@ -1883,7 +1890,7 @@ __get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, > > unsigned long > get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, > - unsigned long pgoff, unsigned long flags) > + unsigned long pgoff, unsigned long flags) Ditto. > { > return __get_unmapped_area(file, addr, len, pgoff, flags, 0); > } > -- > 2.34.1 > -- Kiryl Shutsemau / Kirill A. Shutemov