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; }