Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp136178ybl; Wed, 22 Jan 2020 17:48:07 -0800 (PST) X-Google-Smtp-Source: APXvYqxOO9OreL3B367dnTPKs4ftJkx28QwD6fs4B288b/e5nTYtZWWryBM5IDkiPVZaDudRTk4x X-Received: by 2002:a05:6830:11d2:: with SMTP id v18mr10093843otq.151.1579744087779; Wed, 22 Jan 2020 17:48:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579744087; cv=none; d=google.com; s=arc-20160816; b=y8rVvFmAU7SAbgb5JZU6dj32i90B+GF6S6yOnNu/tyqeXwFnMRNCEXJY4ycYg/Ud8B 8P+tI4BfUtLozTHsZPrU8ZOL+ShPIDgOlNRvPjXTWtJhdMKKRkDY/X7Bpce9XDwLltOQ UaloXrgLkXR3sray6Lqh/zadf0DoXGwY4wgRAac3lhPyKYAt3RZ19wbrlSxD4iCUQxwY kl+hj73De60MyxmvhKAX+aiIe76b+psdvgqPcX7IA12DAdmkSxnimpEX5f3zNIAmnpdF GjgsMrJj67sMUUGp7l4nQlSfWTUjigJV1caFJA1xB7/B0N6qJC2cNE8DPo27HmpYLxgP oWKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=QCHXxNK8/BbuJuJBSwzdO5h2VdmSh01YZxJCYkkB4Qw=; b=hqaU3ayCQUZ1ldDwL91tv52iSSub3eFRncwmHmS/ACdu3YC+zEG0z+bFr0Iwz/v8Gm 0arIsN8piwE3AH1JJXnSgiaMvrS5VWSBrasgF43eFX/MUMQ53umQcxkNR20a5stUKGWJ QAGwa1hrykwJMhlei7tWGXwm2fJWvMuVXaaGpvWRemsFznsIu/yWcwYfXeKnQvXVlG4s rQ+26pSK1C9vot6VbYEV9wbKjFEWbIouKKgeDhqx4oGM5OAe5H+QMQFHFwsk7peMuSQ9 J97OK8MOeGXjqZqJhKbp5D22hZCncwndu/JeHMeESQZyIxJloAga8Kl8GMFO0G7LC1dj DP+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=svRs4+Nn; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 19si142134oiq.128.2020.01.22.17.47.55; Wed, 22 Jan 2020 17:48:07 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=svRs4+Nn; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726083AbgAWBq7 (ORCPT + 99 others); Wed, 22 Jan 2020 20:46:59 -0500 Received: from mail-pj1-f74.google.com ([209.85.216.74]:56863 "EHLO mail-pj1-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725943AbgAWBq6 (ORCPT ); Wed, 22 Jan 2020 20:46:58 -0500 Received: by mail-pj1-f74.google.com with SMTP id d6so561880pjs.6 for ; Wed, 22 Jan 2020 17:46:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=QCHXxNK8/BbuJuJBSwzdO5h2VdmSh01YZxJCYkkB4Qw=; b=svRs4+NnIb8miZBRuWlIo1vn0BvkuOjAkFbeLBRV0SSyde+oZbYjiDCB4uNM5bN0mO DdDMAC88hxUA//KKlSgC9kaUgBcRkpQbKbAKpsyJUNVS8Whp9pUs36g2putQngJj8TQz RGR3CFpXTJSA8JPk3nzeZF26g8idlPT8YXAYLSzLkY0bhiezkAKuDrNC+uRVo3E3b0cf GVfow4dQF9ULUNIr1amkN6ddAaB5PftcRqKchcJQRR81QtyzcWb5oqvgX1UauvBhVvtK nCcBRmh3baRGraWhSGPSUDnVu3WquIUlG7edbRVHYSiAcrVe8sAZ0Fmz9u7bPr4JrZRt ME0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=QCHXxNK8/BbuJuJBSwzdO5h2VdmSh01YZxJCYkkB4Qw=; b=iLi1zJt9EzvA3DY/rUZn8nbJlYqrRVu8snN0W4I2oES8CHVl8oW3if7rAv3K8Nobb7 k2ESZqcNS0ogbcZFF0uKLiJNS8iNgpw+9A2iHenbRoM77y+A92SxrFkAMjK1NCyuQtlO uHCUdKomp6hE4XbMyuleM7EJJdEgefw8H0t7/Hc24rp7QK0PGmkE8hFcgEHTGV1rQTPi iksPkezQNL9sjiy37876AVxUJd5GDywTIKS2t+ubgy0BVIaoBJ6PtsrFx97j1vc9iAvX 8NQvyaGnpWOb/1VjC8q8PJc3Ad/W2d8gnAHYqvsFcgZQY7Sq2ZG/6+oN8lBebx0Faspj X8nw== X-Gm-Message-State: APjAAAVkA7BBM8qvivP3wVPaXiGui4RHZ2RbenSiTSJ43pYnyZy/TUZL huHfpyDY7FdesPTnWhe1kcyqXMUorT3N X-Received: by 2002:a63:e649:: with SMTP id p9mr1318848pgj.15.1579744017943; Wed, 22 Jan 2020 17:46:57 -0800 (PST) Date: Wed, 22 Jan 2020 17:46:27 -0800 Message-Id: <20200123014627.71720-1-bgeffon@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.25.0.341.g760bfbb309-goog Subject: [PATCH] mm: Add MREMAP_DONTUNMAP to mremap(). From: Brian Geffon To: linux-mm@kvack.org Cc: Andrew Morton , "Michael S . Tsirkin" , Arnd Bergmann , Brian Geffon , Sonny Rao , Minchan Kim , Joel Fernandes , Lokesh Gidra , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Yu Zhao , Jesse Barnes Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org MREMAP_DONTUNMAP is an additional flag that can be used with MREMAP_FIXED to move a mapping to a new address. Normally, mremap(2) would then tear down the old vma so subsequent accesses to the vma cause a segfault. However, with this new flag it will keep the old vma with zapping PTEs so any access to the old VMA after that point will result in a pagefault. This feature will find a use in ChromeOS along with userfaultfd. Specifically we will want to register a VMA with userfaultfd and then pull it out from under a running process. By using MREMAP_DONTUNMAP we don't have to worry about mprotecting and then potentially racing with VMA permission changes from a running process. This feature also has a use case in Android, Lokesh Gidra has said that "As part of using userfaultfd for GC, We'll have to move the physical pages of the java heap to a separate location. For this purpose mremap will be used. Without the MREMAP_DONTUNMAP flag, when I mremap the java heap, its virtual mapping will be removed as well. Therefore, we'll require performing mmap immediately after. This is not only time consuming but also opens a time window where a native thread may call mmap and reserve the java heap's address range for its own usage. This flag solves the problem." Signed-off-by: Brian Geffon --- include/uapi/linux/mman.h | 5 +++-- mm/mremap.c | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/include/uapi/linux/mman.h b/include/uapi/linux/mman.h index fc1a64c3447b..923cc162609c 100644 --- a/include/uapi/linux/mman.h +++ b/include/uapi/linux/mman.h @@ -5,8 +5,9 @@ #include #include -#define MREMAP_MAYMOVE 1 -#define MREMAP_FIXED 2 +#define MREMAP_MAYMOVE 1 +#define MREMAP_FIXED 2 +#define MREMAP_DONTUNMAP 4 #define OVERCOMMIT_GUESS 0 #define OVERCOMMIT_ALWAYS 1 diff --git a/mm/mremap.c b/mm/mremap.c index 122938dcec15..bf97c3eb538b 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -318,8 +318,8 @@ unsigned long move_page_tables(struct vm_area_struct *vma, static unsigned long move_vma(struct vm_area_struct *vma, unsigned long old_addr, unsigned long old_len, unsigned long new_len, unsigned long new_addr, - bool *locked, struct vm_userfaultfd_ctx *uf, - struct list_head *uf_unmap) + bool *locked, unsigned long flags, + struct vm_userfaultfd_ctx *uf, struct list_head *uf_unmap) { struct mm_struct *mm = vma->vm_mm; struct vm_area_struct *new_vma; @@ -408,6 +408,13 @@ static unsigned long move_vma(struct vm_area_struct *vma, if (unlikely(vma->vm_flags & VM_PFNMAP)) untrack_pfn_moved(vma); + if (unlikely(!err && (flags & MREMAP_DONTUNMAP))) { + if (vm_flags & VM_ACCOUNT) + vma->vm_flags |= VM_ACCOUNT; + + goto out; + } + if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) { /* OOM: unable to split vma, just get accounts right */ vm_unacct_memory(excess >> PAGE_SHIFT); @@ -422,6 +429,7 @@ static unsigned long move_vma(struct vm_area_struct *vma, vma->vm_next->vm_flags |= VM_ACCOUNT; } +out: if (vm_flags & VM_LOCKED) { mm->locked_vm += new_len >> PAGE_SHIFT; *locked = true; @@ -497,7 +505,7 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr, static unsigned long mremap_to(unsigned long addr, unsigned long old_len, unsigned long new_addr, unsigned long new_len, bool *locked, - struct vm_userfaultfd_ctx *uf, + unsigned long flags, struct vm_userfaultfd_ctx *uf, struct list_head *uf_unmap_early, struct list_head *uf_unmap) { @@ -545,6 +553,17 @@ static unsigned long mremap_to(unsigned long addr, unsigned long old_len, old_len = new_len; } + /* + * MREMAP_DONTUNMAP expands by old_len + (new_len - old_len), we will + * check that we can expand by old_len and vma_to_resize will handle + * the vma growing. + */ + if (unlikely(flags & MREMAP_DONTUNMAP && !may_expand_vm(mm, + vma->vm_flags, old_len >> PAGE_SHIFT))) { + ret = -ENOMEM; + goto out; + } + vma = vma_to_resize(addr, old_len, new_len, &charged); if (IS_ERR(vma)) { ret = PTR_ERR(vma); @@ -561,7 +580,7 @@ static unsigned long mremap_to(unsigned long addr, unsigned long old_len, if (IS_ERR_VALUE(ret)) goto out1; - ret = move_vma(vma, addr, old_len, new_len, new_addr, locked, uf, + ret = move_vma(vma, addr, old_len, new_len, new_addr, locked, flags, uf, uf_unmap); if (!(offset_in_page(ret))) goto out; @@ -609,12 +628,15 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, addr = untagged_addr(addr); new_addr = untagged_addr(new_addr); - if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE)) + if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE | MREMAP_DONTUNMAP)) return ret; if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE)) return ret; + if (flags & MREMAP_DONTUNMAP && !(flags & MREMAP_FIXED)) + return ret; + if (offset_in_page(addr)) return ret; @@ -634,7 +656,8 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, if (flags & MREMAP_FIXED) { ret = mremap_to(addr, old_len, new_addr, new_len, - &locked, &uf, &uf_unmap_early, &uf_unmap); + &locked, flags, &uf, &uf_unmap_early, + &uf_unmap); goto out; } @@ -712,7 +735,7 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, } ret = move_vma(vma, addr, old_len, new_len, new_addr, - &locked, &uf, &uf_unmap); + &locked, flags, &uf, &uf_unmap); } out: if (offset_in_page(ret)) { -- 2.25.0.341.g760bfbb309-goog