From patchwork Tue Mar 23 16:26:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Geffon X-Patchwork-Id: 12158743 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B56E7C433C1 for ; Tue, 23 Mar 2021 16:26:23 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 294BB6198C for ; Tue, 23 Mar 2021 16:26:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 294BB6198C Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 8D90A6B00CE; Tue, 23 Mar 2021 12:26:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 889286B00CF; Tue, 23 Mar 2021 12:26:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 703626B00D0; Tue, 23 Mar 2021 12:26:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0157.hostedemail.com [216.40.44.157]) by kanga.kvack.org (Postfix) with ESMTP id 5222A6B00CE for ; Tue, 23 Mar 2021 12:26:22 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 0E7D518045D1F for ; Tue, 23 Mar 2021 16:26:22 +0000 (UTC) X-FDA: 77951666604.23.5EEE65E Received: from mail-qk1-f201.google.com (mail-qk1-f201.google.com [209.85.222.201]) by imf05.hostedemail.com (Postfix) with ESMTP id DEFFAE000203 for ; Tue, 23 Mar 2021 16:26:18 +0000 (UTC) Received: by mail-qk1-f201.google.com with SMTP id h21so2347288qkl.12 for ; Tue, 23 Mar 2021 09:26:18 -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=lKwKcGb5CUc+F+EMQc/ebBR2BK/GYkxvZ2rPuPPmcByjr2Me5CBw1UfG7iBr8VgnaC c5trrWFa08ivCjAb6j+VX/jq0aIawPSwHEd1C///oS4EGJo+3Pu0M0jKYnACXYUXKu3r UAGTDMgMKWZvKMJ2j2eGtVhnQ8OvL6ws/dqsB9WIPkif7/LkoOPaizMAF6FM+dxMQ0QU 8wcmPNG2PckbeUZ8v054zQRpmV8GnG4zCwlqwu2Vg2rtnFP2ieQTSB1gVWHbzR3swFNH rYY9FXynBlYfikE5RAgTlV7gK8Hkb8Hmzc7D0cd20lrbfcI6fXRRHQco2O893v4iKu5d /mbw== X-Gm-Message-State: AOAM530E4MYYjsbkqVA2OhewCLgwGnXlcGwQvsk1L1dlL7egSLA4xG4A wPkk0OEnO5CfHOc9j/2XA26jxfRrXD6m X-Google-Smtp-Source: ABdhPJwg4rwIUdJnLCHIUMEMPabekLBtDNnkmH0fROuNNU4sOIPxMnlJGgrodI9f9mphy2R854xeGWRJxekc 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 X-Stat-Signature: 3enmnapea3b43tf1ganobzsd4b1r1zbh X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: DEFFAE000203 Received-SPF: none (flex--bgeffon.bounces.google.com>: No applicable sender policy available) receiver=imf05; identity=mailfrom; envelope-from="<3qhZaYAcKCEkmrpqqzyrzzrwp.nzxwty58-xxv6lnv.z2r@flex--bgeffon.bounces.google.com>"; helo=mail-qk1-f201.google.com; client-ip=209.85.222.201 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616516778-850816 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: 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)) From patchwork Tue Mar 23 16:26:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Geffon X-Patchwork-Id: 12158745 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BC3E0C433DB for ; Tue, 23 Mar 2021 16:26:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5E76F6198C for ; Tue, 23 Mar 2021 16:26:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5E76F6198C Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 2BFF06B00CF; Tue, 23 Mar 2021 12:26:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 295F66B00D1; Tue, 23 Mar 2021 12:26:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 10F586B00D3; Tue, 23 Mar 2021 12:26:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0136.hostedemail.com [216.40.44.136]) by kanga.kvack.org (Postfix) with ESMTP id E6BBF6B00CF for ; Tue, 23 Mar 2021 12:26:22 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id A8CDC824999B for ; Tue, 23 Mar 2021 16:26:22 +0000 (UTC) X-FDA: 77951666604.28.643B6F6 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf24.hostedemail.com (Postfix) with ESMTP id 3A534A000857 for ; Tue, 23 Mar 2021 16:26:20 +0000 (UTC) Received: by mail-yb1-f202.google.com with SMTP id t1so3145701ybq.1 for ; Tue, 23 Mar 2021 09:26:21 -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=h1kYRY8AwyqIQ4kqqiLp5b/6u7yV7ANW049MKdfQVdg=; b=VdWRG2j7HcJm5QzBfljdapciRilN8CcW+XERsTmrqR+VQGLKfLF2WoSMukNI7OwrFn j+36dqfjjthvxuoyUTJHAn9mHRcSgOvKg2RisLZnyMZWV7n/SlTcDU+cMZr3vh+wb3T+ 9noHhOIqF4ip2WZlydmrsYRAsBCCwuochvPz97K5+dmWm9UpaCtkYlCH1OnuBeGQbTMg 7wr1FoVPCZqdhsWEqVnjlIxig62LtTxJilm4PPJmwIi4RS/3wDlZi3Lvhq3847voc4wZ ncRw5k6hpnuBgjy86YY475rOUC8usJH5ynYT6sRcd966LLAvTRZs+VP+NjogEFXqcEjz Y4ZA== 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=h1kYRY8AwyqIQ4kqqiLp5b/6u7yV7ANW049MKdfQVdg=; b=AcTKS38kBqO+itgwud3ogmxcgJdSXrUA5IbW7DbTFGFmKU7uLE/wAf4OwMjU0HB6Fx Pd5hXrHx6S1NtltL2s1JXlD3Gcu8l6QN3vrELkXRPkGsNOSYpLqXRCUoYbGBCJJ9UTln xN0uVuf0ZYqtBW8Ad+iwvcMrgbM9ff2GWbS6Zr4iJM1t1OmZBXsrRo1gibxXX1ZgcfOc 6RXHEJ8OOthozfXoR7+7b9+/yUpJq43kV/tZrkJB1a6Uw+XTNNbofkTQHvs8GyJxeGNN AI9UXvGvtgpCfjWuH+qymOdT95MEjTipcJm+aNAIbjaRdoo9zPQFnp6Q0q5eiRLO6XgM /Pug== X-Gm-Message-State: AOAM533zmd9J3igIh3I7Y0I8vihKW2af4b/bPxn1A211RXyu9tKIJRI2 1DGr5Y+4Km4KkwxNo24lWmCXqegO+34D X-Google-Smtp-Source: ABdhPJxgigry6SMhagXnsjKnsxQTkPt5Lf9jprFTGqnLzglZUnY6MQyhwG/04h+AIWvzxzdizmsrvyPW93sk X-Received: from bg.sfo.corp.google.com ([2620:15c:8:10:b5cc:cacb:2875:ae6e]) (user=bgeffon job=sendgmr) by 2002:a25:e0d7:: with SMTP id x206mr6741316ybg.159.1616516780251; Tue, 23 Mar 2021 09:26:20 -0700 (PDT) Date: Tue, 23 Mar 2021 09:26:09 -0700 In-Reply-To: <20210323162611.2398613-1-bgeffon@google.com> Message-Id: <20210323162611.2398613-2-bgeffon@google.com> Mime-Version: 1.0 References: <20210303175235.3308220-1-bgeffon@google.com> <20210323162611.2398613-1-bgeffon@google.com> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog Subject: [PATCH v4 2/3] Revert "mremap: don't allow MREMAP_DONTUNMAP on special_mappings and aio" 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 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3A534A000857 X-Stat-Signature: n6rit3rsjw4rw7wth4fwka5uratmmets Received-SPF: none (flex--bgeffon.bounces.google.com>: No applicable sender policy available) receiver=imf24; identity=mailfrom; envelope-from="<3rBZaYAcKCEsotrss10t11tyr.p1zyv07A-zzx8npx.14t@flex--bgeffon.bounces.google.com>"; helo=mail-yb1-f202.google.com; client-ip=209.85.219.202 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616516780-368687 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: This reverts commit cd544fd1dc9293c6702fab6effa63dac1cc67e99. As discussed in [1] this commit was a no-op because the mapping type was checked in vma_to_resize before move_vma is ever called. This meant that vm_ops->mremap() would never be called on such mappings. Furthermore, we've since expanded support of MREMAP_DONTUNMAP to non-anonymous mappings, and these special mappings are still protected by the existing check of !VM_DONTEXPAND and !VM_PFNMAP which will result in a -EFAULT. 1. https://lkml.org/lkml/2020/12/28/2340 Signed-off-by: Brian Geffon Acked-by: Hugh Dickins --- arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 2 +- fs/aio.c | 5 +---- include/linux/mm.h | 2 +- mm/mmap.c | 6 +----- mm/mremap.c | 2 +- 5 files changed, 5 insertions(+), 12 deletions(-) diff --git a/arch/x86/kernel/cpu/resctrl/pseudo_lock.c b/arch/x86/kernel/cpu/resctrl/pseudo_lock.c index e916646adc69..0daf2f1cf7a8 100644 --- a/arch/x86/kernel/cpu/resctrl/pseudo_lock.c +++ b/arch/x86/kernel/cpu/resctrl/pseudo_lock.c @@ -1458,7 +1458,7 @@ static int pseudo_lock_dev_release(struct inode *inode, struct file *filp) return 0; } -static int pseudo_lock_dev_mremap(struct vm_area_struct *area, unsigned long flags) +static int pseudo_lock_dev_mremap(struct vm_area_struct *area) { /* Not supported */ return -EINVAL; diff --git a/fs/aio.c b/fs/aio.c index 1f32da13d39e..76ce0cc3ee4e 100644 --- a/fs/aio.c +++ b/fs/aio.c @@ -323,16 +323,13 @@ static void aio_free_ring(struct kioctx *ctx) } } -static int aio_ring_mremap(struct vm_area_struct *vma, unsigned long flags) +static int aio_ring_mremap(struct vm_area_struct *vma) { struct file *file = vma->vm_file; struct mm_struct *mm = vma->vm_mm; struct kioctx_table *table; int i, res = -EINVAL; - if (flags & MREMAP_DONTUNMAP) - return -EINVAL; - spin_lock(&mm->ioctx_lock); rcu_read_lock(); table = rcu_dereference(mm->ioctx_table); diff --git a/include/linux/mm.h b/include/linux/mm.h index 64a71bf20536..ecdc6e8dc5af 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -570,7 +570,7 @@ struct vm_operations_struct { void (*close)(struct vm_area_struct * area); /* Called any time before splitting to check if it's allowed */ int (*may_split)(struct vm_area_struct *area, unsigned long addr); - int (*mremap)(struct vm_area_struct *area, unsigned long flags); + int (*mremap)(struct vm_area_struct *area); /* * Called by mprotect() to make driver-specific permission * checks before mprotect() is finalised. The VMA must not diff --git a/mm/mmap.c b/mm/mmap.c index 3f287599a7a3..9d7651e4e1fe 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -3403,14 +3403,10 @@ static const char *special_mapping_name(struct vm_area_struct *vma) return ((struct vm_special_mapping *)vma->vm_private_data)->name; } -static int special_mapping_mremap(struct vm_area_struct *new_vma, - unsigned long flags) +static int special_mapping_mremap(struct vm_area_struct *new_vma) { struct vm_special_mapping *sm = new_vma->vm_private_data; - if (flags & MREMAP_DONTUNMAP) - return -EINVAL; - if (WARN_ON_ONCE(current->mm != new_vma->vm_mm)) return -EFAULT; diff --git a/mm/mremap.c b/mm/mremap.c index db5b8b28c2dd..d22629ff8f3c 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -545,7 +545,7 @@ static unsigned long move_vma(struct vm_area_struct *vma, if (moved_len < old_len) { err = -ENOMEM; } else if (vma->vm_ops && vma->vm_ops->mremap) { - err = vma->vm_ops->mremap(new_vma, flags); + err = vma->vm_ops->mremap(new_vma); } if (unlikely(err)) { From patchwork Tue Mar 23 16:26:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Geffon X-Patchwork-Id: 12158747 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1DF0CC433C1 for ; Tue, 23 Mar 2021 16:26:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A65EF6198C for ; Tue, 23 Mar 2021 16:26:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A65EF6198C Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3D0226B00D1; Tue, 23 Mar 2021 12:26:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A7298D0001; Tue, 23 Mar 2021 12:26:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2495D6B00D5; Tue, 23 Mar 2021 12:26:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0045.hostedemail.com [216.40.44.45]) by kanga.kvack.org (Postfix) with ESMTP id 064946B00D1 for ; Tue, 23 Mar 2021 12:26:28 -0400 (EDT) Received: from smtpin36.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id B08751802E624 for ; Tue, 23 Mar 2021 16:26:27 +0000 (UTC) X-FDA: 77951666814.36.0949B3F Received: from mail-qt1-f202.google.com (mail-qt1-f202.google.com [209.85.160.202]) by imf01.hostedemail.com (Postfix) with ESMTP id 69C595001539 for ; Tue, 23 Mar 2021 16:26:24 +0000 (UTC) Received: by mail-qt1-f202.google.com with SMTP id c20so1543485qtw.9 for ; Tue, 23 Mar 2021 09:26:24 -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=XPRq1W/wuES2HJCP2WXgJHm10YoXyWPHE5kWVTMZXI4=; b=Z2BuAR3KJ1S3vRUxnuAIr23jCEDLS3JCMZT54LNGkWacyQ5/tTVzFNsfhXV/iifyf0 UMYh70Gvk7dGWuubbFlPUZmxni1D3bn7Dbes24AkjDkZIhRl9yC61pAzMKtkgENZx4kq P+PBjD9MqmAGYbHpeZ0eXb0f2gJQaARF2P4aw6hlq0gtwPIEulhe3cHJ+VyoIZZawZ6X 399Z71YgRFMGdFR4U7X7D4jxBeOCuP19SB37meORrEb9mLXtGlOBevosPF7Sp/OENwcK uCCiXL/7rQjdgagMSw6Cd2396aEhXZvrYrO7PQ1RO0jnYXmc/nKMcko3Wd22ptqvvEdb OK6Q== 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=XPRq1W/wuES2HJCP2WXgJHm10YoXyWPHE5kWVTMZXI4=; b=Ia24ZJJW3mVTwnT852K8LA7SL9GisWv6YmUPBI4z1y01iD+V1jBHaLG+DLNODbLrHW ORbK87C5i6ZzopQ0Eovx/ihSwJnA7ZgiuwBWljIjgRjbHZn2heCxLgNB1V41sHzmqPK1 6DOsG1DolIj6T9V8136leXpi84O5SozszDjrJ2r8SwG+Yz+GDmXg25neEbn9aTqkN8ue DuSqVQ8vjdH8dIrExQOMUQJMehyY1TKTpLk4UwzLkrU49aHjR9tMSTPCRfE0VKssbx9i 79FKnmTan0ofzwWQS3SVRQGwxNG87h5Hufk3RPA7+TaJTyxwaYaRk8obtmQglbFPLhr4 piAg== X-Gm-Message-State: AOAM533oq8RfowsfYalwavFn7cIWwdm4RksGByDg2HuFE3KbXjq8AJKj fanSj71oskHfuL/qtWQ0137XEgoFUFom X-Google-Smtp-Source: ABdhPJyGdN/dicqW1iEYZxwWdGQCRoITAp4US/P7dcpV3ycZ/tXYuvuxuUYPrt1uW3ocUDX9KfMawrcaeZ3W X-Received: from bg.sfo.corp.google.com ([2620:15c:8:10:b5cc:cacb:2875:ae6e]) (user=bgeffon job=sendgmr) by 2002:a05:6214:80a:: with SMTP id df10mr6038618qvb.46.1616516782596; Tue, 23 Mar 2021 09:26:22 -0700 (PDT) Date: Tue, 23 Mar 2021 09:26:10 -0700 In-Reply-To: <20210323162611.2398613-1-bgeffon@google.com> Message-Id: <20210323162611.2398613-3-bgeffon@google.com> Mime-Version: 1.0 References: <20210303175235.3308220-1-bgeffon@google.com> <20210323162611.2398613-1-bgeffon@google.com> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog Subject: [PATCH v4 3/3] selftests: Add a MREMAP_DONTUNMAP selftest for shmem 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 X-Stat-Signature: jt8z7izihtxcnxsdo44eupquna8mu5ao X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 69C595001539 Received-SPF: none (flex--bgeffon.bounces.google.com>: No applicable sender policy available) receiver=imf01; identity=mailfrom; envelope-from="<3rhZaYAcKCE0qvtuu32v33v0t.r310x29C-11zAprz.36v@flex--bgeffon.bounces.google.com>"; helo=mail-qt1-f202.google.com; client-ip=209.85.160.202 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616516784-675954 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: This test extends the current mremap tests to validate that the MREMAP_DONTUNMAP operation can be performed on shmem mappings. Signed-off-by: Brian Geffon --- tools/testing/selftests/vm/mremap_dontunmap.c | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tools/testing/selftests/vm/mremap_dontunmap.c b/tools/testing/selftests/vm/mremap_dontunmap.c index 3a7b5ef0b0c6..f01dc4a85b0b 100644 --- a/tools/testing/selftests/vm/mremap_dontunmap.c +++ b/tools/testing/selftests/vm/mremap_dontunmap.c @@ -127,6 +127,57 @@ static void mremap_dontunmap_simple() "unable to unmap source mapping"); } +// This test validates that MREMAP_DONTUNMAP on a shared mapping works as expected. +static void mremap_dontunmap_simple_shmem() +{ + unsigned long num_pages = 5; + + int mem_fd = memfd_create("memfd", MFD_CLOEXEC); + BUG_ON(mem_fd < 0, "memfd_create"); + + BUG_ON(ftruncate(mem_fd, num_pages * page_size) < 0, + "ftruncate"); + + void *source_mapping = + mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE, + MAP_FILE | MAP_SHARED, mem_fd, 0); + BUG_ON(source_mapping == MAP_FAILED, "mmap"); + + BUG_ON(close(mem_fd) < 0, "close"); + + memset(source_mapping, 'a', num_pages * page_size); + + // Try to just move the whole mapping anywhere (not fixed). + void *dest_mapping = + mremap(source_mapping, num_pages * page_size, num_pages * page_size, + MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL); + if (dest_mapping == MAP_FAILED && errno == EINVAL) { + // Old kernel which doesn't support MREMAP_DONTUNMAP on shmem. + BUG_ON(munmap(source_mapping, num_pages * page_size) == -1, + "unable to unmap source mapping"); + return; + } + + BUG_ON(dest_mapping == MAP_FAILED, "mremap"); + + // Validate that the pages have been moved, we know they were moved if + // the dest_mapping contains a's. + BUG_ON(check_region_contains_byte + (dest_mapping, num_pages * page_size, 'a') != 0, + "pages did not migrate"); + + // Because the region is backed by shmem, we will actually see the same + // memory at the source location still. + BUG_ON(check_region_contains_byte + (source_mapping, num_pages * page_size, 'a') != 0, + "source should have no ptes"); + + BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1, + "unable to unmap destination mapping"); + BUG_ON(munmap(source_mapping, num_pages * page_size) == -1, + "unable to unmap source mapping"); +} + // This test validates MREMAP_DONTUNMAP will move page tables to a specific // destination using MREMAP_FIXED, also while validating that the source // remains intact. @@ -300,6 +351,7 @@ int main(void) BUG_ON(page_buffer == MAP_FAILED, "unable to mmap a page."); mremap_dontunmap_simple(); + mremap_dontunmap_simple_shmem(); mremap_dontunmap_simple_fixed(); mremap_dontunmap_partial_mapping(); mremap_dontunmap_partial_mapping_overwrite();