From patchwork Wed Dec 30 07:56:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Xinhai X-Patchwork-Id: 11993025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 30B88C433E0 for ; Wed, 30 Dec 2020 07:57:11 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A7C3121D1B for ; Wed, 30 Dec 2020 07:57:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A7C3121D1B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B9BAD8D00A2; Wed, 30 Dec 2020 02:57:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B4BF38D007F; Wed, 30 Dec 2020 02:57:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A3A7F8D00A2; Wed, 30 Dec 2020 02:57:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0060.hostedemail.com [216.40.44.60]) by kanga.kvack.org (Postfix) with ESMTP id 8AA628D007F for ; Wed, 30 Dec 2020 02:57:09 -0500 (EST) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 3CAAB8249980 for ; Wed, 30 Dec 2020 07:57:09 +0000 (UTC) X-FDA: 77649192978.23.road01_141685d274a3 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin23.hostedemail.com (Postfix) with ESMTP id 18ADE37606 for ; Wed, 30 Dec 2020 07:57:09 +0000 (UTC) X-HE-Tag: road01_141685d274a3 X-Filterd-Recvd-Size: 7153 Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) by imf05.hostedemail.com (Postfix) with ESMTP for ; Wed, 30 Dec 2020 07:57:08 +0000 (UTC) Received: by mail-ed1-f50.google.com with SMTP id b2so14701780edm.3 for ; Tue, 29 Dec 2020 23:57:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=YKfb3I8Ai1v+N6/BF9ZrdSaejKB+AkRAH+yVZt2uZ2E=; b=GXVDJql+upY2RvAy0eVY5gcArf+sdadS/rIZZoXTo42lY3gShKSLGb1M8aZdir38aZ WzTXuUw/mRbewTakOAbZXRugVaCuSVWBkBoQjI76WveK+MbnXoWz7Typuan4Qxw/hgNl PpQAo3HVxmPI9RFjiNm4k36n6pvSJ5ZlaZ0YHskailDXHgRsy8Y/LGaNRPUC6hBAIqs4 R8f6vLDjVJFUEv0BeOZJlk+FNOcflmEpK+PA9E4vPdU43MLvRfbuiPT/ydsqf32YKMyl yFcdtrot/u86gjkCjA7SSbr+ETa3zNwraJDveOuTNlKa2+cx3unB/S0LqRvrex1Uz6KN runw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=YKfb3I8Ai1v+N6/BF9ZrdSaejKB+AkRAH+yVZt2uZ2E=; b=eyIF4sC62P+rsEgRsrPdMAfVJ75DewW7HXbp9RPEaDxJBqu40VaLCIqCYOkRxq33AU pXFv+NEfYrDf0qzBCCkoYk8P4yvFzEX738wcJCCiUoWMl9WttmYW+2BRpsMhWQjufy0o hpZVEjgpsu+HRYX6BpVRGQ54oddPm0rVaTWLzXUCQFmhhoN9hWUg/AwOb61IyKYyfiwv EpPMX9MDnGT61GxavtDNLLEeaVAvcOFrolHmKBBQEVobNiquLJ7V1Fb/r/lFPvB+ihUk LTPre6+GWab3Agj6rYWPkG9vtBUyWVcbva4+NqscTZjM93KxwZHb+BUH0vXnB5kWlF/a wPYw== X-Gm-Message-State: AOAM532E2v/jvW8nbS2VZfh0VyrH+Lj/0V9ahkZanzloPsDidWiEIJG5 rrq5MvzRR4YjqYyyvocjR1IfwJT0aDLm/w== X-Google-Smtp-Source: ABdhPJyucF5i29CFxRIYo2HTKlqz8xMCA0aos3sAXw8wMM2Eh9+mSj7wdIMQ6WxXPU4KudQqFAgp/w== X-Received: by 2002:aa7:d915:: with SMTP id a21mr48841024edr.251.1609315027097; Tue, 29 Dec 2020 23:57:07 -0800 (PST) Received: from localhost.localdomain ([131.228.2.20]) by smtp.gmail.com with ESMTPSA id da9sm37752069edb.84.2020.12.29.23.57.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Dec 2020 23:57:06 -0800 (PST) From: Li Xinhai To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, John Hubbard , "Kirill A. Shutemov" Subject: [PATCH] mm/mremap.c: refactor finding vma and checking vma is alllowed to expand Date: Wed, 30 Dec 2020 15:56:57 +0800 Message-Id: <20201230075657.2720522-1-lixinhai.lxh@gmail.com> X-Mailer: git-send-email 2.18.4 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: Function vma_to_resize)() is called to find the vma to be remapped and also check if expand size is allowed or not. This function assume that all call sites should make sure new_len >= old_len, and currently this assumption is fullfilled at those two call sites, so no real problem at present. After this patch, we explicitly check new_len < old_len case, and separate a new function for checking if expand size is allowed or not. Also rename vma_to_resize to vma_to_remap, since the vma to be remapped would not always require resize. Cc: John Hubbard Cc: "Kirill A. Shutemov" Signed-off-by: Li Xinhai --- mm/mremap.c | 79 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/mm/mremap.c b/mm/mremap.c index c5590afe7165..22eb4e9f35d6 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -621,13 +621,52 @@ static unsigned long move_vma(struct vm_area_struct *vma, return new_addr; } -static struct vm_area_struct *vma_to_resize(unsigned long addr, +static struct vm_area_struct *vma_allow_expand(struct vm_area_struct *vma, + unsigned long addr, unsigned long old_len, unsigned long new_len, + unsigned long *p) +{ + struct mm_struct *mm = current->mm; + unsigned long pgoff; + + pgoff = (addr - vma->vm_start) >> PAGE_SHIFT; + pgoff += vma->vm_pgoff; + if (pgoff + (new_len >> PAGE_SHIFT) < pgoff) + return ERR_PTR(-EINVAL); + + if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)) + return ERR_PTR(-EFAULT); + + if (vma->vm_flags & VM_LOCKED) { + unsigned long locked, lock_limit; + + locked = mm->locked_vm << PAGE_SHIFT; + lock_limit = rlimit(RLIMIT_MEMLOCK); + locked += new_len - old_len; + if (locked > lock_limit && !capable(CAP_IPC_LOCK)) + return ERR_PTR(-EAGAIN); + } + + if (!may_expand_vm(mm, vma->vm_flags, + (new_len - old_len) >> PAGE_SHIFT)) + return ERR_PTR(-ENOMEM); + + if (vma->vm_flags & VM_ACCOUNT) { + unsigned long charged = (new_len - old_len) >> PAGE_SHIFT; + + if (security_vm_enough_memory_mm(mm, charged)) + return ERR_PTR(-ENOMEM); + *p = charged; + } + + return vma; +} + +static struct vm_area_struct *vma_to_remap(unsigned long addr, unsigned long old_len, unsigned long new_len, unsigned long flags, unsigned long *p) { struct mm_struct *mm = current->mm; struct vm_area_struct *vma = find_vma(mm, addr); - unsigned long pgoff; if (!vma || vma->vm_start > addr) return ERR_PTR(-EFAULT); @@ -656,39 +695,11 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr, if (old_len > vma->vm_end - addr) return ERR_PTR(-EFAULT); - if (new_len == old_len) + if (new_len <= old_len) return vma; /* Need to be careful about a growing mapping */ - pgoff = (addr - vma->vm_start) >> PAGE_SHIFT; - pgoff += vma->vm_pgoff; - if (pgoff + (new_len >> PAGE_SHIFT) < pgoff) - return ERR_PTR(-EINVAL); - - if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)) - return ERR_PTR(-EFAULT); - - if (vma->vm_flags & VM_LOCKED) { - unsigned long locked, lock_limit; - locked = mm->locked_vm << PAGE_SHIFT; - lock_limit = rlimit(RLIMIT_MEMLOCK); - locked += new_len - old_len; - if (locked > lock_limit && !capable(CAP_IPC_LOCK)) - return ERR_PTR(-EAGAIN); - } - - if (!may_expand_vm(mm, vma->vm_flags, - (new_len - old_len) >> PAGE_SHIFT)) - return ERR_PTR(-ENOMEM); - - if (vma->vm_flags & VM_ACCOUNT) { - unsigned long charged = (new_len - old_len) >> PAGE_SHIFT; - if (security_vm_enough_memory_mm(mm, charged)) - return ERR_PTR(-ENOMEM); - *p = charged; - } - - return vma; + return vma_allow_expand(vma, addr, old_len, new_len, p); } static unsigned long mremap_to(unsigned long addr, unsigned long old_len, @@ -743,7 +754,7 @@ static unsigned long mremap_to(unsigned long addr, unsigned long old_len, old_len = new_len; } - vma = vma_to_resize(addr, old_len, new_len, flags, &charged); + vma = vma_to_remap(addr, old_len, new_len, flags, &charged); if (IS_ERR(vma)) { ret = PTR_ERR(vma); goto out; @@ -894,7 +905,7 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, /* * Ok, we need to grow.. */ - vma = vma_to_resize(addr, old_len, new_len, flags, &charged); + vma = vma_to_remap(addr, old_len, new_len, flags, &charged); if (IS_ERR(vma)) { ret = PTR_ERR(vma); goto out;