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); From patchwork Wed Sep 26 00:46:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 10615011 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 700DB913 for ; Wed, 26 Sep 2018 00:47:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5FB7D28C99 for ; Wed, 26 Sep 2018 00:47:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 53D73299F6; Wed, 26 Sep 2018 00:47:29 +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 BAF6728C99 for ; Wed, 26 Sep 2018 00:47:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A06C48E0003; Tue, 25 Sep 2018 20:47:24 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 9B70E8E0001; Tue, 25 Sep 2018 20:47:24 -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 8CD768E0003; Tue, 25 Sep 2018 20:47:24 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by kanga.kvack.org (Postfix) with ESMTP id 4FB0B8E0001 for ; Tue, 25 Sep 2018 20:47:24 -0400 (EDT) Received: by mail-pf1-f199.google.com with SMTP id x19-v6so13666723pfh.15 for ; Tue, 25 Sep 2018 17:47:24 -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:in-reply-to:references; bh=VY6/NKAq4co41CFYNxC8WKUaHQrGRNHhEphxcDhT01s=; b=AeJinevmno/X29OKRnBHn8yaE0m7KApLJ0g19pg2zvm3Q8S/ycYXSFGx7gL2ZoVnlU t8DJ86RNkJI84Ire+7d85ZvCXhsokiqVcGbLgWyrXszAs95Dhmb40JIMV8ojkZTr74sl 56Hf//mlUBMV/dsfe/wruunGbsEdYhbVApRUhRF79UV9okUorIXsVTo+K1vRaFt1LOev vulFLc7IeiGyAuZa2ZBjEvTghhS8YDxbNSpe4TanCnf/Mp0vdpGJlMJ8azgjbLBVRAnF jTeFgKAbn+OLUIBROxGCGTIW7xMB5IXFxtQsf1xkKyfibcgXzhuOcb073DMl70QsWUgO Rhkg== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.130 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: ABuFfoiYO2Lco5aSbGQenCLcVhY6kU8K3JxoQ96ZttRLGZ6z00hnpPvr 2+NxAmoVfCCc1gv7s9phHqxTApuSmXDAxiCJ9P2iVSXntpZf+BFF7WhaMrdEC0PQ3VIaoegSbvc H2bQ0MsRRcSoBFDGOfoiKzreWvPH6h3ABIp2C1mo0xdAV31gJiCRTabB/Sie+FIS/ew== X-Received: by 2002:a63:b104:: with SMTP id r4-v6mr3321858pgf.442.1537922843880; Tue, 25 Sep 2018 17:47:23 -0700 (PDT) X-Google-Smtp-Source: ACcGV63PBjLeP8KjCscbRZk4TL3arJp8pHxhQx7k7Z8UAi4B6X0/+HUw1Ew+VHEh0CSF5Sqeq5Jg X-Received: by 2002:a63:b104:: with SMTP id r4-v6mr3321818pgf.442.1537922842594; Tue, 25 Sep 2018 17:47:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537922842; cv=none; d=google.com; s=arc-20160816; b=oNxqDLqOslO0AxkDQpqdAbyAi+O/YfCuv4nmXq6x2ZlqtppWBDjSNKgB3acivJVTPJ xPUP1v++mTvZAF+5YGqmL1uT6iyfdYXRYOnU/+6/Kzsl6b7w1M4d434yq27K57d9SC5w PYfjhGpjuHUs30eLkosbn4tDw7fFHLh1WumOOqouYZNEjWvzPHwZTjicuf5mt0NIHKwx eCoXZFY+AsMqO/kGnxwRT6GkFISbKjUeVG9jfbo+doDW/OW6moncgo4zc6SJh3XuToI/ qszfZnEnRzVgXaPIDp1L4s+0V59e52dm/5BH7/2BRNJw5T+UmLCeVhIfO8Hh1/2czcTf rbqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=VY6/NKAq4co41CFYNxC8WKUaHQrGRNHhEphxcDhT01s=; b=Vj4kAnL07YZeVRl7Cc+YqtkrrM1wmqovzQrc57gtuXGkiAYTOyYTtoH9TYOGbyqR/M rwthjd8m7N1EE6Ba7zp/sIDr9bGrywWni1xrnYNDZBtsdtHYMgxLjHUM00QPuwgnQR7q 5pw3PNZEtVS5kd+OCMI3nCfPpM75g/M3xwrV7lHRZMMuZ6ik6MU6CO5FllCJeZIMMlCM mrNen5gfgR1Bb+jcEL1gzsnwNLhz7YQlHYGUQ2dvXmIPrTdJmSp880wihhKlEmZt1M4o nV53jrxDb3+PAqWuSaONbhsdMhD/JlsxR8Q0TaunKLSaBVEdLvuMm3RBj/L8Kvh06vb3 Jcdg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.130 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-130.freemail.mail.aliyun.com (out30-130.freemail.mail.aliyun.com. [115.124.30.130]) by mx.google.com with ESMTPS id b124-v6si3770044pgc.45.2018.09.25.17.47.21 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Sep 2018 17:47:22 -0700 (PDT) Received-SPF: pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.130 as permitted sender) client-ip=115.124.30.130; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.130 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=01201311R181e4;CH=green;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01f04455;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 2/2 -mm] mm: brk: dwongrade mmap_sem to read when shrinking Date: Wed, 26 Sep 2018 08:46:56 +0800 Message-Id: <1537922816-108051-2-git-send-email-yang.shi@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1537922816-108051-1-git-send-email-yang.shi@linux.alibaba.com> References: <1537922816-108051-1-git-send-email-yang.shi@linux.alibaba.com> 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 brk might be used to shinrk memory mapping too. Use __do_munmap() to shrink mapping with downgrading mmap_sem to read. Cc: Michal Hocko Cc: Kirill A. Shutemov Cc: Matthew Wilcox Cc: Laurent Dufour Cc: Vlastimil Babka Cc: Andrew Morton Signed-off-by: Yang Shi --- mm/mmap.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 017bcfa..3da14a1 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -193,9 +193,11 @@ static int do_brk_flags(unsigned long addr, unsigned long request, unsigned long unsigned long retval; unsigned long newbrk, oldbrk; struct mm_struct *mm = current->mm; + unsigned long origbrk = mm->brk; struct vm_area_struct *next; unsigned long min_brk; bool populate; + bool downgrade = false; LIST_HEAD(uf); if (down_write_killable(&mm->mmap_sem)) @@ -229,14 +231,26 @@ static int do_brk_flags(unsigned long addr, unsigned long request, unsigned long newbrk = PAGE_ALIGN(brk); oldbrk = PAGE_ALIGN(mm->brk); - if (oldbrk == newbrk) - goto set_brk; + if (oldbrk == newbrk) { + mm->brk = brk; + goto success; + } /* Always allow shrinking brk. */ if (brk <= mm->brk) { - if (!do_munmap(mm, newbrk, oldbrk-newbrk, &uf)) - goto set_brk; - goto out; + /* + * mm->brk need to be protected by write mmap_sem, update it + * before downgrading mmap_sem. + * When __do_munmap fail, it will be restored from origbrk. + */ + mm->brk = brk; + retval = __do_munmap(mm, newbrk, oldbrk-newbrk, &uf, true); + if (retval < 0) { + mm->brk = origbrk; + goto out; + } else if (retval == 1) + downgrade = true; + goto success; } /* Check against existing mmap mappings. */ @@ -247,18 +261,21 @@ static int do_brk_flags(unsigned long addr, unsigned long request, unsigned long /* Ok, looks good - let it rip. */ if (do_brk_flags(oldbrk, newbrk-oldbrk, 0, &uf) < 0) goto out; - -set_brk: mm->brk = brk; + +success: populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0; - up_write(&mm->mmap_sem); + if (downgrade) + up_read(&mm->mmap_sem); + else + up_write(&mm->mmap_sem); userfaultfd_unmap_complete(mm, &uf); if (populate) mm_populate(oldbrk, newbrk - oldbrk); return brk; out: - retval = mm->brk; + retval = origbrk; up_write(&mm->mmap_sem); return retval; }