From patchwork Wed Sep 26 00:46:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 10615013 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2C71414BD for ; Wed, 26 Sep 2018 00:47:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1DA4328C99 for ; Wed, 26 Sep 2018 00:47:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 11D60299C9; Wed, 26 Sep 2018 00:47:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 77197298DF for ; Wed, 26 Sep 2018 00:47:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 796508E0004; Tue, 25 Sep 2018 20:47:25 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 746868E0001; Tue, 25 Sep 2018 20:47:25 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 65CBF8E0004; Tue, 25 Sep 2018 20:47:25 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) by kanga.kvack.org (Postfix) with ESMTP id 2A6848E0001 for ; Tue, 25 Sep 2018 20:47:25 -0400 (EDT) Received: by mail-pf1-f198.google.com with SMTP id v9-v6so13590155pff.4 for ; Tue, 25 Sep 2018 17:47:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id; bh=ArLksz0U4yaGi1+Lo2q+QJ4o99K/GpnqyMuyjJ9mqjE=; b=LMOtg8JKaB4VvBLxX5AuOcz9NXwrxzxm1DI2Rvf+7pmvpNkKTVxGQJ4gpIVZpFk/XL W327j2cyeJNE6iLH2WeYUluGBsE5JjYqq/LslB1LyylG8lnG+GnVhjcKYnTbjnNrglPe Aprr4s9QMdidVI2jsWCXhOzA+2OUvBeaazyjA3jnjwVQjM0TnaOLTnsi8JAKFBZsBiKu 9I94S0TRz94md7bJEB1wWvNfWhJQU2ALPaIDH1YdW7kwHX2a8UajBlPUL+lT8cD1Yg1j 9jA+fMTCSkI7KD8bzXaQqwrshT2HSBlnRXRcsrDVXuSIdYRElvKMog7LeeNpB45mS/sl 71sA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.132 as permitted sender) smtp.mailfrom=yang.shi@linux.alibaba.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alibaba.com X-Gm-Message-State: ABuFfojBR74brA7LyyAX/ySOrDNcMge6ushlKXpgQsP1IXOWUBh4nRpM sW7nCbUfkNRyXSh8cxhwaglRBY3kqG0ce0gz9zCR+jkOfJu6ZGrU0drAdh1abBXQck2wI39gJ2w T0VgfcWHLTIq5PYKAyTW2EYLPZeMop00wpbpzY/udJL2kUjxZ8+SuO9HIAVlWgmShdQ== X-Received: by 2002:a63:6445:: with SMTP id y66-v6mr3275214pgb.443.1537922844839; Tue, 25 Sep 2018 17:47:24 -0700 (PDT) X-Google-Smtp-Source: ACcGV61gQS4/aXoVk/EyItUQoEWVBq7QY26e/zKeJP+xRJaZaNiIU2bWnBzzknl5b/m0bkohwwOT X-Received: by 2002:a63:6445:: with SMTP id y66-v6mr3275157pgb.443.1537922843414; Tue, 25 Sep 2018 17:47:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537922843; cv=none; d=google.com; s=arc-20160816; b=jkIUg5s9lvM5b+Eb+8ksU5G/Zgr6xyaqQOMn0g3sNIydWhZveRISEBDUy5FX3b8+21 PTMKxFNoE1MGAdpHJpBk7muoGpNdhgZK/Ygh7VwKJuknDXV8rZcLps6o1qbSv/CAB5sc sd3aIgAJHmSHNA+Oos99jUTaxdvTMW4z7oNxTgZQIhQADB7Dt+9/dFLICXWzCKnN1RIR c8rhoSyWGWfCeD6Q87pBJA85Uhb8Olb741mOOTpSofgtT65/oG3KWuHuBE0k5tNo9Dft RUFfKhUiW4OBtFHRDatYehEyJB3RyaBqw52wTHCHqUVJRcSiuaFCejrfeiDEIDTJ8uqr 9KFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from; bh=ArLksz0U4yaGi1+Lo2q+QJ4o99K/GpnqyMuyjJ9mqjE=; b=vqPoE55E9rZgr+GyY3WkE3UzGFzknixXUKwtrZqNLYuzqx3HAK8IXeKRVjk98qXPd5 RENLbw/bHivam9sl9tY3P8sIf4HkHkxOkQ+Op5EqrciA/jjKUnt56eJUBVGF22JxOEQd sCGt4IpUiqIOB6i1Be79sit0nXc7IVWvDj2xYoLhF04o0XEH2SZjdSuoujtU6Mc9a8MJ f/EGdcgVYN5tnHuhFGPvZKG7pE3zpN/u5s3XlMoXcuUEXBjYSMv2uAo/J3fgHUvlW2ub J8AtGyjUkO9W5ZR/eY5GQMtLNIKlL0jc0mk51ZlDKsyPXfjkQwqDoCX1N+CaBRY3yzGY ww0A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.132 as permitted sender) smtp.mailfrom=yang.shi@linux.alibaba.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from out30-132.freemail.mail.aliyun.com (out30-132.freemail.mail.aliyun.com. [115.124.30.132]) by mx.google.com with ESMTPS id w15-v6si3817114pgg.529.2018.09.25.17.47.22 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Sep 2018 17:47:23 -0700 (PDT) Received-SPF: pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.132 as permitted sender) client-ip=115.124.30.132; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.132 as permitted sender) smtp.mailfrom=yang.shi@linux.alibaba.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alibaba.com X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R171e4;CH=green;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e07402;MF=yang.shi@linux.alibaba.com;NM=1;PH=DS;RN=9;SR=0;TI=SMTPD_---0T9TF2nJ_1537922816; Received: from e19h19392.et15sqa.tbsite.net(mailfrom:yang.shi@linux.alibaba.com fp:SMTPD_---0T9TF2nJ_1537922816) by smtp.aliyun-inc.com(127.0.0.1); Wed, 26 Sep 2018 08:47:08 +0800 From: Yang Shi To: mhocko@kernel.org, kirill@shutemov.name, willy@infradead.org, ldufour@linux.vnet.ibm.com, vbabka@suse.cz, akpm@linux-foundation.org Cc: yang.shi@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2 -mm] mm: mremap: dwongrade mmap_sem to read when shrinking Date: Wed, 26 Sep 2018 08:46:55 +0800 Message-Id: <1537922816-108051-1-git-send-email-yang.shi@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 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: X-Virus-Scanned: ClamAV using ClamSMTP Other than munmap, mremap might be used to shrink memory mapping too. Use __do_munmap() to shrink mapping with downgrading mmap_sem to read. MREMAP_FIXED and MREMAP_MAYMOVE are more complicated to adopt this optimization since they need manipulate vmas after do_munmap(), downgrading mmap_sem may create race window. Simple mapping shrink is the low hanging fruit, and it may cover the most cases of unmap with munmap. Cc: Michal Hocko Cc: Kirill A. Shutemov Cc: Matthew Wilcox Cc: Laurent Dufour Cc: Vlastimil Babka Cc: Andrew Morton Signed-off-by: Yang Shi --- include/linux/mm.h | 2 ++ mm/mmap.c | 4 ++-- mm/mremap.c | 16 ++++++++++++---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index a61ebe8..3028028 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2286,6 +2286,8 @@ extern unsigned long do_mmap(struct file *file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, vm_flags_t vm_flags, unsigned long pgoff, unsigned long *populate, struct list_head *uf); +extern int __do_munmap(struct mm_struct *, unsigned long, size_t, + struct list_head *uf, bool downgrade); extern int do_munmap(struct mm_struct *, unsigned long, size_t, struct list_head *uf); diff --git a/mm/mmap.c b/mm/mmap.c index 847a17d..017bcfa 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2687,8 +2687,8 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, * work. This now handles partial unmappings. * Jeremy Fitzhardinge */ -static int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len, - struct list_head *uf, bool downgrade) +int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len, + struct list_head *uf, bool downgrade) { unsigned long end; struct vm_area_struct *vma, *prev, *last; diff --git a/mm/mremap.c b/mm/mremap.c index 5c2e185..e608e92 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -525,6 +525,7 @@ static int vma_expandable(struct vm_area_struct *vma, unsigned long delta) unsigned long ret = -EINVAL; unsigned long charged = 0; bool locked = false; + bool downgrade = false; struct vm_userfaultfd_ctx uf = NULL_VM_UFFD_CTX; LIST_HEAD(uf_unmap_early); LIST_HEAD(uf_unmap); @@ -561,12 +562,16 @@ static int vma_expandable(struct vm_area_struct *vma, unsigned long delta) /* * Always allow a shrinking remap: that just unmaps * the unnecessary pages.. - * do_munmap does all the needed commit accounting + * __do_munmap does all the needed commit accounting */ if (old_len >= new_len) { - ret = do_munmap(mm, addr+new_len, old_len - new_len, &uf_unmap); - if (ret && old_len != new_len) + ret = __do_munmap(mm, addr+new_len, old_len - new_len, + &uf_unmap, true); + if (ret < 0 && old_len != new_len) goto out; + /* Returning 1 indicates mmap_sem is downgraded to read. */ + else if (ret == 1) + downgrade = true; ret = addr; goto out; } @@ -631,7 +636,10 @@ static int vma_expandable(struct vm_area_struct *vma, unsigned long delta) vm_unacct_memory(charged); locked = 0; } - up_write(¤t->mm->mmap_sem); + if (downgrade) + up_read(¤t->mm->mmap_sem); + else + up_write(¤t->mm->mmap_sem); if (locked && new_len > old_len) mm_populate(new_addr + old_len, new_len - old_len); userfaultfd_unmap_complete(mm, &uf_unmap_early);