From patchwork Wed Oct 11 17:04:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13417648 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3F37BCDB46A for ; Wed, 11 Oct 2023 17:04:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1DF3E8D0112; Wed, 11 Oct 2023 13:04:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 168C48D0050; Wed, 11 Oct 2023 13:04:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E852A8D0112; Wed, 11 Oct 2023 13:04:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CCFF18D0050 for ; Wed, 11 Oct 2023 13:04:45 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 82A9C1A035E for ; Wed, 11 Oct 2023 17:04:45 +0000 (UTC) X-FDA: 81333804930.02.1229685 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) by imf01.hostedemail.com (Postfix) with ESMTP id 7FC4C40028 for ; Wed, 11 Oct 2023 17:04:43 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=aPP4pq2R; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf01.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.48 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697043883; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=BvzEZsVg5isoEN6IAS+eP4mzWmRlP3jKSau9yDceX7A=; b=QDcz37sKs3F2kP/7VN2kVfTQt+4oyH4E2Rae7pEbb8kHIvLJLHBx8kW6oBUQwid9VRdoEc QlUX128H7ry/O9bMSK6XcGKlVwKeM/IXd4HlZU2g3nZH8A2zNtvrPQ67kIkRXf97QQaN+E D6wxXgRAHklLyW//oskcKogfX0L/tMc= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=aPP4pq2R; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf01.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.48 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697043883; a=rsa-sha256; cv=none; b=6PRIaz9DIIDMFQZ5oO33jNHuD7PzZFgM3Sk7ASLKhp7rZUJX5AofbDwiLWZDONHl1aK8WN dPcoKC6kdeEAIC2Vs6Xd42FVlWazQIs+wlcaU75ZdxHvNYUiFqhPeSuLhvlGZupVsj6eqe OtkP0mB7nbfRV1r48GdVVAXZs8t4TYw= Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-40566f8a093so955495e9.3 for ; Wed, 11 Oct 2023 10:04:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697043881; x=1697648681; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BvzEZsVg5isoEN6IAS+eP4mzWmRlP3jKSau9yDceX7A=; b=aPP4pq2RfcRoS8ZxhB9HkN4qbJ3J7GkvXsddnGdnGGj5famDXPmw3jha3wDLOs6UF/ iGVIYho6jEN6GNILp2yHfq0tSN7qIeQAumaayFrCcyTxSr4ieRWGmtSSQ4C+jM7+/k+i W/l/C5koFQ1z1UAn0SUQmaO87xDfsMZwxRyYYiVoCNA2XCz6gt+qA6+v2NbU27E+sws9 1x5EGLTBK+8zmnOi/xtvYfN3KdG2ooLL8iyW9H/7P4bCJSj+kRa/hHCred4+82oJGp9W Izs9J/y2H4XWDBcMZy+h1HbtSI+7H2GrZxJZFylqnGdhQOtmQ3JqE6XGitTHgCqRU2by x/Pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697043881; x=1697648681; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BvzEZsVg5isoEN6IAS+eP4mzWmRlP3jKSau9yDceX7A=; b=MwcO557DZXQ2gVZIBCXPYcHCSInRgnYzw3htzUcxn9dhy27nm3NeVOPp1z8Q1WL7ko yEidnvx9eDKlwgWNjLQzh7WPg9C9aWwWSmCjgQADoXg/0k9ucR1SwRYw0eeGw5N5MyLf MfK+CZjS9nBzN7mNPnpB5QE4xrmBwP/9VynpzAtPXGfwjmb5/Bu/5kovONTL1D2paxVu v/B7i5pBKjg1lYbyTyt7k4MZxoxRn2z9s93nvQd7OjEx8Em7LIgtjaIaby+5/urSbwRu 9DldeD9GtDa6ZNs4KP+YY0eFklj+qSxp8uLF/PZM/9Zi2H0Y5FM4dfe/V62R99Mjx8pS jf7g== X-Gm-Message-State: AOJu0YxRLVIft8aVZYo2zdhLN51KXkTva9mX+3I7JLwBXAQ7DJQsdKDn +9FtV1XI7Sfx3P7ZbVjBOL7OtJDCJqw= X-Google-Smtp-Source: AGHT+IGrlRJzT4WiuJiYQVl4Nooi7hmIa8OfY69qFoOPIJKNCAos7O7P28vtT2EcH8CnCemJHwPBzQ== X-Received: by 2002:a7b:c394:0:b0:403:31a:77b9 with SMTP id s20-20020a7bc394000000b00403031a77b9mr19163325wmj.37.1697043881511; Wed, 11 Oct 2023 10:04:41 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id y19-20020a05600c20d300b004075b3ce03asm3834495wmm.6.2023.10.11.10.04.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 10:04:40 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Alexander Viro , Christian Brauner Cc: "=Liam R . Howlett" , Vlastimil Babka , linux-fsdevel@vger.kernel.org, Lorenzo Stoakes Subject: [PATCH v4 5/5] mm: abstract VMA merge and extend into vma_merge_extend() helper Date: Wed, 11 Oct 2023 18:04:31 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 7FC4C40028 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: e8c4bz86qd9mwt5gansu88aksmi6oxek X-HE-Tag: 1697043883-326317 X-HE-Meta: U2FsdGVkX1/xMBP99QEKrGpDcs7c/wYfQImi5P1wHWGk60uLfQX02UCq90Xnw38FIDYPJwB1ILXeJpvVqPHlk4bxjotrECEOikaJO8eaYPHof0kecw/q1JgE4KRxkuZh8lBEsRa+Ds3DOJe2/yQKq+OTvwYYLpRqiHaYjC7p2plfPQa+DgFPXsFFfqquvKG4VHXE1reVS3HH+vGI1gRxObfGeNTQSZp0FkzniXl/6L4oX3Jz/R9wEa1L4GaRD0bzYLm4kFlJS4qNqYvCgKlAonywHQ+tWqb3ssV6HBJKR2So7BZQSaEszW/e9+gvcOK0kKbwctc70LOJOibnrk+2QkZsnP44Y3gbSDUtcBOlRMiP6S046O7wJaIOeHj50Ydq52Nmm2fu1Je4l2v9aXkdySqHecOuY4LdtxavQPQsTW1bqfxDlyd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2U+KY9kJgYEJESbtDZ5Jn2CSdSrVNZKcrDakiIgs= 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: mremap uses vma_merge() in the case where a VMA needs to be extended. This can be significantly simplified and abstracted. This makes it far easier to understand what the actual function is doing, avoids future mistakes in use of the confusing vma_merge() function and importantly allows us to make future changes to how vma_merge() is implemented by knowing explicitly which merge cases each invocation uses. Note that in the mremap() extend case, we perform this merge only when old_len == vma->vm_end - addr. The extension_start, i.e. the start of the extended portion of the VMA is equal to addr + old_len, i.e. vma->vm_end. With this refactoring, vma_merge() is no longer required anywhere except mm/mmap.c, so mark it static. Reviewed-by: Vlastimil Babka Signed-off-by: Lorenzo Stoakes --- mm/internal.h | 8 +++----- mm/mmap.c | 31 ++++++++++++++++++++++++------- mm/mremap.c | 30 +++++++++++++----------------- 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index ddaeb9f2d9d7..6fa722b07a94 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1014,11 +1014,9 @@ struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, /* * mm/mmap.c */ -struct vm_area_struct *vma_merge(struct vma_iterator *vmi, - struct mm_struct *, struct vm_area_struct *prev, unsigned long addr, - unsigned long end, unsigned long vm_flags, struct anon_vma *, - struct file *, pgoff_t, struct mempolicy *, struct vm_userfaultfd_ctx, - struct anon_vma_name *); +struct vm_area_struct *vma_merge_extend(struct vma_iterator *vmi, + struct vm_area_struct *vma, + unsigned long delta); enum { /* mark page accessed */ diff --git a/mm/mmap.c b/mm/mmap.c index e5e50e547ebf..3ea52451623b 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -860,13 +860,13 @@ can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, * **** is not represented - it will be merged and the vma containing the * area is returned, or the function will return NULL */ -struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, - struct vm_area_struct *prev, unsigned long addr, - unsigned long end, unsigned long vm_flags, - struct anon_vma *anon_vma, struct file *file, - pgoff_t pgoff, struct mempolicy *policy, - struct vm_userfaultfd_ctx vm_userfaultfd_ctx, - struct anon_vma_name *anon_name) +static struct vm_area_struct +*vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, + struct vm_area_struct *prev, unsigned long addr, unsigned long end, + unsigned long vm_flags, struct anon_vma *anon_vma, struct file *file, + pgoff_t pgoff, struct mempolicy *policy, + struct vm_userfaultfd_ctx vm_userfaultfd_ctx, + struct anon_vma_name *anon_name) { struct vm_area_struct *curr, *next, *res; struct vm_area_struct *vma, *adjust, *remove, *remove2; @@ -2499,6 +2499,23 @@ static struct vm_area_struct vma->vm_userfaultfd_ctx, anon_vma_name(vma)); } +/* + * Expand vma by delta bytes, potentially merging with an immediately adjacent + * VMA with identical properties. + */ +struct vm_area_struct *vma_merge_extend(struct vma_iterator *vmi, + struct vm_area_struct *vma, + unsigned long delta) +{ + pgoff_t pgoff = vma->vm_pgoff + vma_pages(vma); + + /* vma is specified as prev, so case 1 or 2 will apply. */ + return vma_merge(vmi, vma->vm_mm, vma, vma->vm_end, vma->vm_end + delta, + vma->vm_flags, vma->anon_vma, vma->vm_file, pgoff, + vma_policy(vma), vma->vm_userfaultfd_ctx, + anon_vma_name(vma)); +} + /* * do_vmi_align_munmap() - munmap the aligned region from @start to @end. * @vmi: The vma iterator diff --git a/mm/mremap.c b/mm/mremap.c index ce8a23ef325a..38d98465f3d8 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -1096,14 +1096,12 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, /* old_len exactly to the end of the area.. */ if (old_len == vma->vm_end - addr) { + unsigned long delta = new_len - old_len; + /* can we just expand the current mapping? */ - if (vma_expandable(vma, new_len - old_len)) { - long pages = (new_len - old_len) >> PAGE_SHIFT; - unsigned long extension_start = addr + old_len; - unsigned long extension_end = addr + new_len; - pgoff_t extension_pgoff = vma->vm_pgoff + - ((extension_start - vma->vm_start) >> PAGE_SHIFT); - VMA_ITERATOR(vmi, mm, extension_start); + if (vma_expandable(vma, delta)) { + long pages = delta >> PAGE_SHIFT; + VMA_ITERATOR(vmi, mm, vma->vm_end); long charged = 0; if (vma->vm_flags & VM_ACCOUNT) { @@ -1115,17 +1113,15 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, } /* - * Function vma_merge() is called on the extension we - * are adding to the already existing vma, vma_merge() - * will merge this extension with the already existing - * vma (expand operation itself) and possibly also with - * the next vma if it becomes adjacent to the expanded - * vma and otherwise compatible. + * Function vma_merge_extend() is called on the + * extension we are adding to the already existing vma, + * vma_merge_extend() will merge this extension with the + * already existing vma (expand operation itself) and + * possibly also with the next vma if it becomes + * adjacent to the expanded vma and otherwise + * compatible. */ - vma = vma_merge(&vmi, mm, vma, extension_start, - extension_end, vma->vm_flags, vma->anon_vma, - vma->vm_file, extension_pgoff, vma_policy(vma), - vma->vm_userfaultfd_ctx, anon_vma_name(vma)); + vma = vma_merge_extend(&vmi, vma, delta); if (!vma) { vm_unacct_memory(charged); ret = -ENOMEM;