Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp4391555pxf; Tue, 23 Mar 2021 09:28:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwdHYrm8eCEZH3Ot/2QAeG7qlnRVZqMfywrTxbxrjfGwyHRSkRg01pABfqnad0AQC2ymZaa X-Received: by 2002:aa7:c3c1:: with SMTP id l1mr5503362edr.208.1616516895517; Tue, 23 Mar 2021 09:28:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616516895; cv=none; d=google.com; s=arc-20160816; b=p+8bEcvxOD1FVeidYTSaB292RURt8v2gHcYc1JY47iiG4WVZumV+sfZFT/KtDT5Y0A V1bcLhScoVnIt7crWXTm/Yj4sX6duHNkFRKqIZgkTKwVnauWUn2pK7vShGhdNMx9o6kj vvXGYbrSwgSUNg0o37FvE5V/PJQznzYZUHJXoEx5MW9VUzKOa9/kCFGMMI3b5aG8HWeE Oy4Hn0BvXcCftX6M0Zhb5XjMzs+Q4gStyQfl0JZ5Ky0kAGyy1M5yYcQe6CGD4OHwz3lM GI6A3w9++OYiNe5y5VxjIJhXP3lJNbGo8A1ulx1aRXaMOMsFSeHT9g5YGQkmjk5EDV7M l6Uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=n6MgZRmjRkHGAKz5NOP9vxE91Mkgm+fsdWg3ns4pBws=; b=FeAbyo5YiN6dIW0mhEre8qjmoPVWqKTr/e8hgeQgHD96i9W83ZLdEf7BrIgu7954l+ GKtG7quwsWtqahBx3alNn4xDPXNfocBXLoNwYkoXav6kkkNs+WtEXMhLOJUPKocvmCZP MPppCXBqpRAttaPxD/4xI/YWZ5HpG8dkw2jfFsconR9AwWKEGXJGInaWVWFfZFndL2T4 4BDfTIGm6VsIH3DyhL1rbuRmGX1N3bL9bEDhT2K/bGJYC+FPTniT8IOnaaB1t+1D7IVX bJwSD3RzqGVTG3y4RD7mKJ1wzfDwkKpQ15WhXQj06pEzqzra8aJHxld8husStbKBcAlQ ywVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=sh5yoDsF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id f1si13668902ejh.95.2021.03.23.09.27.51; Tue, 23 Mar 2021 09:28:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=sh5yoDsF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S233254AbhCWQ04 (ORCPT + 99 others); Tue, 23 Mar 2021 12:26:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233225AbhCWQ0U (ORCPT ); Tue, 23 Mar 2021 12:26:20 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2059FC061574 for ; Tue, 23 Mar 2021 09:26:19 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id c1so2358038qke.8 for ; Tue, 23 Mar 2021 09:26:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=n6MgZRmjRkHGAKz5NOP9vxE91Mkgm+fsdWg3ns4pBws=; b=sh5yoDsFJETNBe8FClnjRA7HtDwQu/2+xa0OK9DvIZJzuEAskE0IqX6jSN1JnJUJHU iSIF71CO8jHw0/iO5gp76xvblL1xa0wgYP8d7R01aAzKjO4Tk+Hzzh2kXRpQh52/PecS La77iujaazzkcIEBfG8MBZtk8fWdnvypTrEyardClPL3a5BfrSMgRGci5h7BQwVgdBGG D+8bFfBX9y8XTvdi0LwgLkPrBBRzPZ/7KaWCdm5+KQgTwiLb6RAb9NyicLjxPXkDpnBD 96LIyZVyy2xAIcaAEZoKMvTUIvSG6QCO1BoFI7a8bTE50vWvmrj1PxdLF5d93WYAEWW3 K7rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=n6MgZRmjRkHGAKz5NOP9vxE91Mkgm+fsdWg3ns4pBws=; b=mZx+uk9iGRfXJiNUdAXd2L6y5dK3qRznMUq9ZrOjJKkhRG5TuvVWmJnXT+MDO2y0hu YFfdu8oRavbfpIjFgocM5EWKhA6vaQ62NQs9bZq/RmtQMvz/wzf/UdLxvfQZNyoxYWua o8pJZsXzU6pLavUMNtOgzH9bu0ky/nIcEKnPF4CsTAPCKAX2zQEoSUumCchLntfPtmZ3 eYeY2S/5TlhGTDgIrR0fCSG9DHL4GeiF7UsRI0Skaa7n8Psli187eUnqTlIoqrIMgZEf 4E7U2Sc6XiJSDWJYXJzy47uu7W7vgwyCf0Zz4oOaBRKQQK2qnsl+d56eQsyh0BEO1sVm lqsQ== X-Gm-Message-State: AOAM5338zgG9VkUEUdjwTF1Zao1597pemRNwLA6orsAZlv3ObFbGrfXg EHjuTbLLbGeTmOevXPzGZmUVR+MTLaXS X-Received: from bg.sfo.corp.google.com ([2620:15c:8:10:b5cc:cacb:2875:ae6e]) (user=bgeffon job=sendgmr) by 2002:ad4:57a5:: with SMTP id g5mr5599168qvx.60.1616516778194; Tue, 23 Mar 2021 09:26:18 -0700 (PDT) Date: Tue, 23 Mar 2021 09:26:08 -0700 In-Reply-To: <20210303175235.3308220-1-bgeffon@google.com> Message-Id: <20210323162611.2398613-1-bgeffon@google.com> Mime-Version: 1.0 References: <20210303175235.3308220-1-bgeffon@google.com> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog Subject: [PATCH v4 1/3] mm: Extend MREMAP_DONTUNMAP to non-anonymous mappings From: Brian Geffon To: Andrew Morton Cc: Axel Rasmussen , Lokesh Gidra , Mike Rapoport , Peter Xu , Hugh Dickins , "Michael S . Tsirkin" , Brian Geffon , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andy Lutomirski , Vlastimil Babka , Andrea Arcangeli , Sonny Rao , Minchan Kim , "Kirill A . Shutemov" , Dmitry Safonov , Michael Kerrisk , Alejandro Colomar Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently MREMAP_DONTUNMAP only accepts private anonymous mappings. This restriction was placed initially for simplicity and not because there exists a technical reason to do so. This change will widen the support to include any mappings which are not VM_DONTEXPAND or VM_PFNMAP. The primary use case is to support MREMAP_DONTUNMAP on mappings which may have been created from a memfd. This change will result in mremap(MREMAP_DONTUNMAP) returning -EINVAL if VM_DONTEXPAND or VM_PFNMAP mappings are specified. Lokesh Gidra who works on the Android JVM, provided an explanation of how such a feature will improve Android JVM garbage collection: "Android is developing a new garbage collector (GC), based on userfaultfd. The garbage collector will use userfaultfd (uffd) on the java heap during compaction. On accessing any uncompacted page, the application threads will find it missing, at which point the thread will create the compacted page and then use UFFDIO_COPY ioctl to get it mapped and then resume execution. Before starting this compaction, in a stop-the-world pause the heap will be mremap(MREMAP_DONTUNMAP) so that the java heap is ready to receive UFFD_EVENT_PAGEFAULT events after resuming execution. To speedup mremap operations, pagetable movement was optimized by moving PUD entries instead of PTE entries [1]. It was necessary as mremap of even modest sized memory ranges also took several milliseconds, and stopping the application for that long isn't acceptable in response-time sensitive cases. With UFFDIO_CONTINUE feature [2], it will be even more efficient to implement this GC, particularly the 'non-moveable' portions of the heap. It will also help in reducing the need to copy (UFFDIO_COPY) the pages. However, for this to work, the java heap has to be on a 'shared' vma. Currently MREMAP_DONTUNMAP only supports private anonymous mappings, this patch will enable using UFFDIO_CONTINUE for the new userfaultfd-based heap compaction." [1] https://lore.kernel.org/linux-mm/20201215030730.NC3CU98e4%25akpm@linux-foundation.org/ [2] https://lore.kernel.org/linux-mm/20210302000133.272579-1-axelrasmussen@google.com/ Signed-off-by: Brian Geffon Acked-by: Hugh Dickins Tested-by: Lokesh Gidra --- mm/mremap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/mremap.c b/mm/mremap.c index ec8f840399ed..db5b8b28c2dd 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -653,8 +653,8 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr, return ERR_PTR(-EINVAL); } - if (flags & MREMAP_DONTUNMAP && (!vma_is_anonymous(vma) || - vma->vm_flags & VM_SHARED)) + if ((flags & MREMAP_DONTUNMAP) && + (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))) return ERR_PTR(-EINVAL); if (is_vm_hugetlb_page(vma)) -- 2.31.0.rc2.261.g7f71774620-goog