From patchwork Tue Oct 10 18:23:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13415854 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 CD3CECD8CAF for ; Tue, 10 Oct 2023 18:23:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B8408D00DC; Tue, 10 Oct 2023 14:23:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 467E88D0002; Tue, 10 Oct 2023 14:23:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E2458D00DC; Tue, 10 Oct 2023 14:23:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 1A8388D0002 for ; Tue, 10 Oct 2023 14:23:31 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C28C6C02D2 for ; Tue, 10 Oct 2023 18:23:30 +0000 (UTC) X-FDA: 81330374580.06.8670913 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) by imf01.hostedemail.com (Postfix) with ESMTP id CCC444001D for ; Tue, 10 Oct 2023 18:23:28 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=l2rkSSdC; spf=pass (imf01.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.47 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696962208; 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=tFbKpQ6SsRHnWdu3vxpCFtAo2jAJKsxmAumC1+QExjY=; b=C8LShBusb6BJPeTBWSPV49xO8+9DnJ3gZpdC2n7eRteOJa45sMHdiChvyrUEIKQoESxcy3 yTN4pkmJGvQWATPN4NSEw4up7I+MTLAETJpsoRlc7DDY2GXoOf8LeqK+6Jw/3NS3KEt4RE mfcIPtKRufI2/Zua7NlR0g9QMkNj70U= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696962208; a=rsa-sha256; cv=none; b=k0+w7ZG3FtKZWG7b5Wcr+448dBiwWyDqbpmiKT6mRRNOTm+Bu6Ba8IliK9B/hjqOBmLwk6 MR2yRjSHXiJUXGtdklRpXSUPKcx5d5QplYpTMqMSmmwknPsX9u+8n0eyty98NqOvysbdV5 6khx0fhFYtSHxlgEIur2fOIsq1OsG/s= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=l2rkSSdC; spf=pass (imf01.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.47 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-40535597f01so56318375e9.3 for ; Tue, 10 Oct 2023 11:23:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696962207; x=1697567007; 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=tFbKpQ6SsRHnWdu3vxpCFtAo2jAJKsxmAumC1+QExjY=; b=l2rkSSdCCrzm6UePc3kIaE3aCV6sTRzfkUW1EOHZ/i+/mGYVl2/ogfJSUW/pVjqsKf PXTJQxK9nTpppZvosuJvzRnsTA+te1xuyP3pibXNC1LhKB5fs6nt0nus5pthprMfL+KB l++D+t+mJ/7KYkeTUjrWoB1ppfvpNPPitpm8qyVT5pmvTGXUrCGGCnx6wjeWCduIQAeg zy/BtKzG72wTgsRhbWY1VhPxqmmQqbqZYTpYWxNjTcfW4kCnwb5pll76UNZ2Pk9P4z+j 8XTumwcjhycTCRHpkZ0IAGYkydLizPOXnnAbt3l6zelhB8/WTB8zMengUZehODD4QqQk lt4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696962207; x=1697567007; 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=tFbKpQ6SsRHnWdu3vxpCFtAo2jAJKsxmAumC1+QExjY=; b=ioFBvB/NggRDkmLo/a4ygP9sXpsd0cPHnk1UFZXHBhgz/4U8gkDSd0Tw+CJTqwwSeG zWR3WC3Nl87Z8EC+WCovMJfAK816WKr6wsmrSgFNkaePHmE6b3qcjF6cbAS6lcTmlq6w wYB5B600Y4nf/AUz/VVB3qOG7S1uN2tn7Lu6P5UOaGSuoNZXLDHj/9PGXdZ24XI+Po39 stlN00JmWGYJasaIaubAZvIK/+ped6oRRBeg8ckFaRO6uArPa8X/VKB4OFsDfz2DxJgY lX6x0noOAYdQ1IBMNd9xWelg19+vx07df7EZhT9AkKLn6vHmPM/VuvmkYGJ0ZiZeHVRR iwgg== X-Gm-Message-State: AOJu0YxCThr3DoT5RGUBr3maSsO1roxcJDJaz7NF7CEP7VdP2dubn4Zc ILg7ZCbQY03LQHE+kLstG04nt3l9xGA= X-Google-Smtp-Source: AGHT+IHv1LOl5HKi62e1HGQi+SAntjdzxRBlNr+hjRg05MokHXX/rE8OMoNY9iwhCLEYBLbo73Wz5A== X-Received: by 2002:a05:6000:1cca:b0:320:8d6:74f5 with SMTP id bf10-20020a0560001cca00b0032008d674f5mr15754499wrb.28.1696962207032; Tue, 10 Oct 2023 11:23:27 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id j16-20020a5d6190000000b003217cbab88bsm13225312wru.16.2023.10.10.11.23.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 11:23:25 -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 v3 5/5] mm: abstract VMA merge and extend into vma_merge_extend() helper Date: Tue, 10 Oct 2023 19:23:08 +0100 Message-ID: <8134bfa4b1accb98645be3e8cb2af8ac5a92c469.1696929425.git.lstoakes@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: grsbb73hiqwo1x5wzeipisp1uqwiw6os X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: CCC444001D X-Rspam-User: X-HE-Tag: 1696962208-624960 X-HE-Meta: U2FsdGVkX18Rj/ENkQREW+xpNTdyOk8zsAdXOJPDLSzgHwzyINu+lQXB9d8vl9Aui7a3uGSEqQSdC8qStQt/+6JZq2T/0Jq5pWycxzVdZlClcUGF8GoJzc5V/RAojeV23lJxrJ4AZYBCMAMm0f19sSfNyijrvzb1T/HG6GKDDZpBHJhc6u3IPuSCZFxZgS2XLZLoWym2FMtBZ35r/wHxuBQgX/pfruR4QyyAn7yZC7a8ggSHVW93njA0M+SP9l0f0bxo5+DbOUd9jDCAcwIvbSprvp/TywbR+ocXqE+X988ILJRt8HnrNf3bf1ZPTuzHme/4TqweaVzWGJgCnYkRwA6JqcBKRkLqrymU63G/yQhkMj7RT5kL0J/k8CI2Nle6/QGTIW4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ASWKcNc1zHaBC7wYgsoU01N5EIEHVMzDqKVpDSa0xXLiZNK4+8wYjtsYOfp3qY3jDXpzHDA737Qf1JR2Kq772vis4w= 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 db3842601a88..fb0b8f2b8010 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; @@ -2501,6 +2501,23 @@ static struct vm_area_struct *vma_merge_new_vma(struct vma_iterator *vmi, 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;