From patchwork Thu Aug 9 23:36:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 10562131 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 6D9E01057 for ; Thu, 9 Aug 2018 23:36:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5B873289DD for ; Thu, 9 Aug 2018 23:36:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4F56D2BB3A; Thu, 9 Aug 2018 23:36:35 +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 2D62B289DD for ; Thu, 9 Aug 2018 23:36:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B11D16B0003; Thu, 9 Aug 2018 19:36:32 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id AC2D26B0005; Thu, 9 Aug 2018 19:36:32 -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 9B3376B0007; Thu, 9 Aug 2018 19:36:32 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) by kanga.kvack.org (Postfix) with ESMTP id 5BEFC6B0003 for ; Thu, 9 Aug 2018 19:36:32 -0400 (EDT) Received: by mail-pg1-f198.google.com with SMTP id w23-v6so3519606pgv.1 for ; Thu, 09 Aug 2018 16:36:32 -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=2201v2MmNuRtwjB5F8oqWVU8vl5q9Dp42OYAHY53gsU=; b=fG+t6GHiAJXilSH7JqtN/BGXgfg5GZxZKwbm2XLNRVT/2KtANEB1cic2gzUxncbg3B qX61peVdkMQa0D0CMSTYdhsJ1/zH4f7Lu/byMY2HgXu71RFMqc7EYT446T7OeYWQQroP poo/4lqpsfaCJ9U7bd4ckQMrtSxfhlR7PKzMtO134p0wuQ1xeaynpXIpX8J53wdIvyzh I2AUxGAHf/cq89q3xnjY8hd6uLKycAPKn8IH3I+F2i/OgISsQ262IN0xWYmIvo6KWOQq bR5Uj1UyUDZJOUHH9XuWtlYcLuKOPZjBqLlvWnhiN41Yf7uy+IB+/wtD8NBOuPuQnIgJ RgUA== 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: AOUpUlGu3nOH6Ul8frX2OgioShHHgzbrc2hJI/aQgsoJ8r4hsL2+bsuC uR4vPjmDnwFXfwXTd+KGBBnNxHAuHoYs24q+W47bIQWteSOKAoLa277LLbBh0wqtx5iaPo2U6TJ zjsqEwUQU2wWadyZ9N6bqWAJIfNSJrTKyR0YLxgr1+lVYcMXKgbxUD5ihv2hqSAhZpA== X-Received: by 2002:a62:11c4:: with SMTP id 65-v6mr4414941pfr.54.1533857792003; Thu, 09 Aug 2018 16:36:32 -0700 (PDT) X-Google-Smtp-Source: AA+uWPx1ATe8gJ/MKubprxEppVpMrUnhDCLdvaaV0TP3w8iMlEb/No7FY33043JcYO9ewd+l4c9+ X-Received: by 2002:a62:11c4:: with SMTP id 65-v6mr4414891pfr.54.1533857790871; Thu, 09 Aug 2018 16:36:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533857790; cv=none; d=google.com; s=arc-20160816; b=rQma/oO1VVZGAG74UiqQskXow77PUpRpU18TiXhZo8oCWqXQjXsAm5pBzyqemg5Ifm RLJkMTT8AwNy8tt2tKX6AIqv0qZD9e6hW+3k5mRn3BHyRb7VlJ4gkkPHaBPOAAcONeuj 0wKglE8Wle73qK3q9Dh7S8opxQ55BUM1ETH88CLWm3IZMYHuiXj5VJCLA9ZOj2APLoEo eKDJ5qdHc+0CBNroUaefErBKbbDec23nsW69HcLRebhXkXHc5UcSYaziQeksBhE6V5yU EiHyFsCvzCoXj0cCldhnJmaaGLYiHsegSeIXY3B3PqDLOaDVMP2FOytf6ZzvzaS3881u ra6g== 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 :arc-authentication-results; bh=2201v2MmNuRtwjB5F8oqWVU8vl5q9Dp42OYAHY53gsU=; b=Y0wAwtYs2EC9uLE5lsvx2mRkIMcR8AD8qahzTyRPJ/+Jn4IVtV1VcS3wqJhwPcIClZ o9SPcazlqRVnXvnj5jMsszmiJRrLtxIva+R94THtmkG9FYAzGMTZVfKvhXEBVU8Yby9R q6IwsB8BJEX1Hg4Sut720nH56Z6odgeIPVxZGbo0PxDOIObNDG7zqwDdhECYfDDabwTc 0MEn3CUn3iJVnxLPtm8vUek333jVMEohVygZeu/qToG3k8UgynAvYGotakCiohjLrV08 uZw8/gOB+DlAejEGk/Nm7pnl+n/2hhbEf+OIJbr7BDGlStsc8Ie6RYVSx+ZY2c3CGUin wVng== 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 h130-v6si7941113pfe.119.2018.08.09.16.36.30 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 09 Aug 2018 16:36:30 -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=01201311R191e4;CH=green;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e01451;MF=yang.shi@linux.alibaba.com;NM=1;PH=DS;RN=15;SR=0;TI=SMTPD_---0T6MWc.W_1533857777; Received: from e19h19392.et15sqa.tbsite.net(mailfrom:yang.shi@linux.alibaba.com fp:SMTPD_---0T6MWc.W_1533857777) by smtp.aliyun-inc.com(127.0.0.1); Fri, 10 Aug 2018 07:36:26 +0800 From: Yang Shi To: mhocko@kernel.org, willy@infradead.org, ldufour@linux.vnet.ibm.com, kirill@shutemov.name, vbabka@suse.cz, akpm@linux-foundation.org, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org Cc: yang.shi@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC v7 PATCH 1/4] mm: refactor do_munmap() to extract the common part Date: Fri, 10 Aug 2018 07:36:00 +0800 Message-Id: <1533857763-43527-2-git-send-email-yang.shi@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1533857763-43527-1-git-send-email-yang.shi@linux.alibaba.com> References: <1533857763-43527-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 Signed-off-by: Yang Shi Acked-by: Vlastimil Babka --- mm/mmap.c | 100 ++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 29 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 17bbf4d..2a6898b 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2681,35 +2681,40 @@ 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 + * + * returns the pointer to vma, NULL or err ptr when spilt_vma returns error. + */ +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. @@ -2727,11 +2732,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; } @@ -2740,10 +2745,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 @@ -2764,13 +2812,7 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, */ 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; - } + munlock_vmas(tmp, end); } /*