From patchwork Tue Sep 11 20:58:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 10596315 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 6D802112B for ; Tue, 11 Sep 2018 20:58:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5E4D129DC1 for ; Tue, 11 Sep 2018 20:58:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5282D29DCF; Tue, 11 Sep 2018 20:58:41 +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 20F2929DC1 for ; Tue, 11 Sep 2018 20:58:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CDEAC8E0006; Tue, 11 Sep 2018 16:58:36 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C671D8E0001; Tue, 11 Sep 2018 16:58:36 -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 B03B58E0006; Tue, 11 Sep 2018 16:58:36 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) by kanga.kvack.org (Postfix) with ESMTP id 6FA6B8E0001 for ; Tue, 11 Sep 2018 16:58:36 -0400 (EDT) Received: by mail-pf1-f197.google.com with SMTP id d22-v6so13445272pfn.3 for ; Tue, 11 Sep 2018 13:58:36 -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=mdrLuO88nuRuyNRN/IyrXfz40XhoiZdKXx4T6zLnk3k=; b=Rzn7QlK7NtfTl9vcKOaKGbfpCrd3tAQmK6NZDHIhr9BOlMKolSnbkmPyGhAWaUSw3A MAx9uIopjw2JFUES8pBqCzJPab4sddNGZ8Zv43eP0fgVNJDb/ksjtAFy26C4stR4UX8X gnKDzcdZIChnCXanpU2GVvtg585GpwZnpP5MZ0Y81zqgcmrTx467W6JL46MbTAN72LWq rfNY5I4jP0LnUSZyW5ATpP24WblLkhlBHko61w0m9fAmSZCQXfQKlwy5PufceZ715SFQ 2shlPwt9XXtY796R2RETe1lwAZjg0nWDnpu+sxbV9Z0kfiaRPGlP0X5HFbc/lpe2NmEo ukTA== 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: APzg51Bpz1+yxct7UM+dQGb1O3b2UuSThpTHs3J2Bq96J0zSWSm+hXK7 EO1BXcJ76AN20WuAQYzKjm64aJ1sU4rGY1O4AbenmkkVMwlCD+Nzax1G4Wx3GaaieZAjS/VLr4o ivgFCzo2NM9fQ4DhJgoGDQIgEFI/InzO1RckfnRKhOsMuBmE8wSML+Rd0k01T9qFQYQ== X-Received: by 2002:a17:902:7447:: with SMTP id e7-v6mr29170061plt.186.1536699516128; Tue, 11 Sep 2018 13:58:36 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZf7IrY2NEX2jqNNGZKJn2GFJJs9Tw8AMrHEBOcauq2uEHrK+dEHxdrjV09Z9feq0OPNdJg X-Received: by 2002:a17:902:7447:: with SMTP id e7-v6mr29169998plt.186.1536699514817; Tue, 11 Sep 2018 13:58:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536699514; cv=none; d=google.com; s=arc-20160816; b=V9Um+5NAZTcJiWgF7HNHH8XAj4fJBXSzKvAVrjUOvBzkTRhG4QrdG1zTFcdOdeYprg GRjU3Es0RmaNLIUqYx2uot67HhgZT0O5X55ovaxhSCxL80Wmvq9pwNOIDFESnTm5kE/L ksQi4ghgEjCImMs4jMT/MgrnRbQtfqwF/vHWqn5T1QSnHBG24F17/vk7Qv4BOk2hmJjN KsGAq9EiKLHVyh4LKULXz5SR2s70h8skzi8hN1IfHCjq1yo/f7pvLr8R2/qaXa8ceo3B vN4vJvK3RMVFU0p+3Y8bnoh8hYbIumFqZ1esCXzs/IHutOzt5V5aWbuSj8SqRRRIXc85 VOqA== 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=mdrLuO88nuRuyNRN/IyrXfz40XhoiZdKXx4T6zLnk3k=; b=tntT37WLaBuJApXl3VgXype05Cxkvlom4r9E5SjyEsgKNsQRrBYxw2VRD8AeRW0NQl qRRF6xHEdYppzDkWdZ2H+LVfq42qvleoH+6NrtsvhjEykEixYzU5sCJn0jVLtQSyukPw EbW6r7N1WWR2MVCVUXD67thSswbHOA7ASjuP7qM/6M/83UiqhK/+C2v+EzJlnHdv0wC4 2Br1Uzc+y96nbZgAplq+ZjIEOaaQ+iStmbRxdVorzhHi8uPyJwmUuc7sg+wMBdzWXAQ1 mlqGg0rl07fCbl2NAjv3fTc1tOcv15ocWKSmVtKlfdi8XrOJDXhTuq4uGELgS7/Ievi0 Oclw== 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 1-v6si20025473plv.28.2018.09.11.13.58.34 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Sep 2018 13:58:34 -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=01201311R131e4;CH=green;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04400;MF=yang.shi@linux.alibaba.com;NM=1;PH=DS;RN=11;SR=0;TI=SMTPD_---0T8Sg6Yy_1536699501; Received: from e19h19392.et15sqa.tbsite.net(mailfrom:yang.shi@linux.alibaba.com fp:SMTPD_---0T8Sg6Yy_1536699501) by smtp.aliyun-inc.com(127.0.0.1); Wed, 12 Sep 2018 04:58:29 +0800 From: Yang Shi To: mhocko@kernel.org, willy@infradead.org, ldufour@linux.vnet.ibm.com, vbabka@suse.cz, akpm@linux-foundation.org, dave.hansen@intel.com, oleg@redhat.com, srikar@linux.vnet.ibm.com Cc: yang.shi@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC v9 PATCH 1/4] mm: refactor do_munmap() to extract the common part Date: Wed, 12 Sep 2018 04:58:10 +0800 Message-Id: <1536699493-69195-2-git-send-email-yang.shi@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1536699493-69195-1-git-send-email-yang.shi@linux.alibaba.com> References: <1536699493-69195-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 Introduces three new helper functions: * addr_ok() * munmap_lookup_vma() * munlock_vmas() They will be used by do_munmap() and the new do_munmap with zapping large mapping early in the later patch. There is no functional change, just code refactor. Reviewed-by: Laurent Dufour Acked-by: Vlastimil Babka Signed-off-by: Yang Shi --- mm/mmap.c | 106 +++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 74 insertions(+), 32 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 5f2b2b1..b7092b4 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2682,35 +2682,42 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, return __split_vma(mm, vma, addr, new_below); } -/* Munmap is split into 2 main parts -- this part which finds - * what needs doing, and the areas themselves, which do the - * work. This now handles partial unmappings. - * Jeremy Fitzhardinge - */ -int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, - struct list_head *uf) +static inline bool addr_ok(unsigned long start, size_t len) { - unsigned long end; - struct vm_area_struct *vma, *prev, *last; - if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start) - return -EINVAL; + return false; - len = PAGE_ALIGN(len); - if (len == 0) - return -EINVAL; + if (PAGE_ALIGN(len) == 0) + return false; + + return true; +} + +/* + * munmap_lookup_vma: find the first overlap vma and split overlap vmas. + * @mm: mm_struct + * @start: start address + * @end: end address + * + * Return: %NULL if no VMA overlaps this range. An ERR_PTR if an + * overlapping VMA could not be split. Otherwise a pointer to the first + * VMA which overlaps the range. + */ +static struct vm_area_struct *munmap_lookup_vma(struct mm_struct *mm, + unsigned long start, unsigned long end) +{ + struct vm_area_struct *vma, *prev, *last; /* Find the first overlapping VMA */ vma = find_vma(mm, start); if (!vma) - return 0; - prev = vma->vm_prev; - /* we have start < vma->vm_end */ + return NULL; + /* we have start < vma->vm_end */ /* if it doesn't overlap, we have nothing.. */ - end = start + len; if (vma->vm_start >= end) - return 0; + return NULL; + prev = vma->vm_prev; /* * If we need to split any vma, do it now to save pain later. @@ -2728,11 +2735,11 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, * its limit temporarily, to help free resources as expected. */ if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) - return -ENOMEM; + return ERR_PTR(-ENOMEM); error = __split_vma(mm, vma, start, 0); if (error) - return error; + return ERR_PTR(error); prev = vma; } @@ -2741,10 +2748,53 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, if (last && end > last->vm_start) { int error = __split_vma(mm, last, end, 1); if (error) - return error; + return ERR_PTR(error); } vma = prev ? prev->vm_next : mm->mmap; + return vma; +} + +static inline void munlock_vmas(struct vm_area_struct *vma, + unsigned long end) +{ + struct mm_struct *mm = vma->vm_mm; + + while (vma && vma->vm_start < end) { + if (vma->vm_flags & VM_LOCKED) { + mm->locked_vm -= vma_pages(vma); + munlock_vma_pages_all(vma); + } + vma = vma->vm_next; + } +} + +/* Munmap is split into 2 main parts -- this part which finds + * what needs doing, and the areas themselves, which do the + * work. This now handles partial unmappings. + * Jeremy Fitzhardinge + */ +int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, + struct list_head *uf) +{ + unsigned long end; + struct vm_area_struct *vma, *prev; + + if (!addr_ok(start, len)) + return -EINVAL; + + len = PAGE_ALIGN(len); + + end = start + len; + + vma = munmap_lookup_vma(mm, start, end); + if (!vma) + return 0; + if (IS_ERR(vma)) + return PTR_ERR(vma); + + prev = vma->vm_prev; + if (unlikely(uf)) { /* * If userfaultfd_unmap_prep returns an error the vmas @@ -2763,16 +2813,8 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, /* * unlock any mlock()ed ranges before detaching vmas */ - if (mm->locked_vm) { - struct vm_area_struct *tmp = vma; - while (tmp && tmp->vm_start < end) { - if (tmp->vm_flags & VM_LOCKED) { - mm->locked_vm -= vma_pages(tmp); - munlock_vma_pages_all(tmp); - } - tmp = tmp->vm_next; - } - } + if (mm->locked_vm) + munlock_vmas(vma, end); /* * Remove the vma's, and unmap the actual pages