From patchwork Mon Oct 9 20:53:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13414433 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 AF6CDCD612F for ; Mon, 9 Oct 2023 20:53:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1B68F8D0092; Mon, 9 Oct 2023 16:53:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 168528D0089; Mon, 9 Oct 2023 16:53:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 006598D0092; Mon, 9 Oct 2023 16:53:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id E34138D0089 for ; Mon, 9 Oct 2023 16:53:43 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id B18641203AF for ; Mon, 9 Oct 2023 20:53:43 +0000 (UTC) X-FDA: 81327124326.08.0BFCBF3 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) by imf26.hostedemail.com (Postfix) with ESMTP id CD68C140008 for ; Mon, 9 Oct 2023 20:53:41 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=F3DSM57D; spf=pass (imf26.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.52 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=1696884821; 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=xL574zJuwTg3rbZ38h4Jjm16YvUMoLxu5gzcSq0qQog=; b=0vcgMvtp/sfGL6yyrFdgZs8LFCNf7k5RcKbvtaCX/f7lHGvfUSxVMl3Fa3pMG2veIQBMjQ X2iNj68NBrSHDsRqUy9KFpEm77FBrZuzAFTVkdOrCzsf6CC9afJudcMJUbPVwnEULdf3po Ruk1OoqeT4IXFRV7dFwZoC1FAWRraL0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696884821; a=rsa-sha256; cv=none; b=CBP64kPoDwN56I4V7gtoqdlszo4rdkyv1PfHFs2Rg3cjGXqGzlfBRLJEv9qtt4YQkDtiPy Y8XKSMjEWfYzcwaXA/kz8QyKn/PVi9J5zNI+tnOuQ1HtrqR8zhpeNPuLdWwXRTLbuS+PnT P23SIYzgVaXDF1iAhIyoFWRmATiA3VY= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=F3DSM57D; spf=pass (imf26.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.52 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-327be5fe4beso4567197f8f.3 for ; Mon, 09 Oct 2023 13:53:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696884820; x=1697489620; 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=xL574zJuwTg3rbZ38h4Jjm16YvUMoLxu5gzcSq0qQog=; b=F3DSM57DrnIKfRwOzB0B3uGklGhxr1DIOO6P3t9FxfHe72KB7cTj269WKQwKwhzPEr GYAbJxPXayYYRgkVohjzdIxGrN5P/mXA+j012XFCXTuD4Wly2/v5vq2uAoeQqS6BOqeT suGuAUIIDpmJvjhx2rKmCUXNbD8v1IjUqe7J2COMoUgTjcntmrEaBNcYk3Ec+PYA3gOZ AyPhbm4J1nCJ8rFk0CoLW0uosewRGKi1hqQLLq+sTHwvIc2QwAkyRWmDrXlD/jauW9I1 t28T2Ygftv3NliJglwGWA5M/IjlwQ3y53etdWjSnPb4nD2xHTvIbs+edTu8HD6bWhP3X 3sfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696884820; x=1697489620; 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=xL574zJuwTg3rbZ38h4Jjm16YvUMoLxu5gzcSq0qQog=; b=Gttz+5Uwu0HCFEPUQGfWwz8NtwUCUQICX4hbiKoqGtyJzC37ruzfNd+p80ZhylA/Ym sWKvpOlLmJ+iiEWr451qIlPzJwPB2J8XXi8opt640AmX9EkGDa1+WuISUgguH0gXDBOq B3p+ujZz77veL9rPWo/9rtqsuGTPYakajWO/8NifC6cXa9YzZrZ7KN81hojmoyVyTici afiQLo7bsUmNcfqmAcr2MxRhKHcI5INp7TuVJN4TehT3kiNN1/3I9LXsyV1B8yjaP78l 9uDO1d5ovPdl9x+S2CRuZbwshGBDoNov+QV5VxKeBih+Oai8A7m4so30BxzjKRXK0El3 MrpA== X-Gm-Message-State: AOJu0YxsYFG2JSEGTtY1M3VlNQTcuxZy00X/gNdjPzp1JF3kFdil5RNP yiVPLsKHN2XyAgoVsGH9tl3Lv39OUlY= X-Google-Smtp-Source: AGHT+IEZcDJXfUk/m4asVym6HC8MzhKstd/QCfQCxF3CdaRPkCYRP2UAGVNJPPmMrmT3c3m/Jt2IXw== X-Received: by 2002:adf:e6ce:0:b0:31f:b7b2:d4e1 with SMTP id y14-20020adfe6ce000000b0031fb7b2d4e1mr14332666wrm.20.1696884819954; Mon, 09 Oct 2023 13:53:39 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id l2-20020a5d4802000000b0031fe0576460sm10578130wrq.11.2023.10.09.13.53.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 13:53:39 -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 v2 5/5] mm: abstract VMA merge and extend into vma_merge_extend() helper Date: Mon, 9 Oct 2023 21:53:20 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: CD68C140008 X-Rspam-User: X-Stat-Signature: q43zr1xzcpo17gsoaeqxbjd1dfmod5xz X-Rspamd-Server: rspam03 X-HE-Tag: 1696884821-724163 X-HE-Meta: U2FsdGVkX18G1j4ufzAhhHVAsJluJXvd7ZreD5V2HxY4mJwwK78VRqhmZ/Mi4KSX7H/fnhovfrJZfQqBzq5mofT6OQDwC82IdFEdVB07H5MglSj5Jq77kwph79DsoauYzFbhgQGEaxOo4DwvtbY6uCw5Na9pBxlcTY5mjHjGuXNMRN0ZkViFBstx7GEgA1cXCkoqDeegreEzvzud2tIIlqRT5vVChDnGxrI/mKFuAcdcXWiMPfkhGJjEloNuXSHyRJBR2DJDGN9rXLaRjzSn391gfpPtNzo2D4r6GXwbSabxX/DnBglkDyzW6UfrHkxfI+UMZ2vouqpW3fYbXWDCrB5jiOf9O7hhneT/PcQ6KbWVNt20clVMFxl+9X0oEFL6Mso7RL3FfYeqxkxl5RMosf8OOrvEDAzmU5mFj3l+5H/MPa5U26ahlApKgDeJ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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 33aafd23823b..200319bf3292 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; @@ -2498,6 +2498,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;