From patchwork Tue Oct 10 18:23:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13415853 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 94FE0CD8CB0 for ; Tue, 10 Oct 2023 18:23:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2A1888D00DB; Tue, 10 Oct 2023 14:23:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F88E8D0002; Tue, 10 Oct 2023 14:23:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 07A4D8D00DB; Tue, 10 Oct 2023 14:23:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E6AD68D0002 for ; Tue, 10 Oct 2023 14:23:28 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 9F7AB1202C4 for ; Tue, 10 Oct 2023 18:23:28 +0000 (UTC) X-FDA: 81330374496.07.CD0AB24 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) by imf09.hostedemail.com (Postfix) with ESMTP id C07CD140009 for ; Tue, 10 Oct 2023 18:23:26 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FmNYxWtb; spf=pass (imf09.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.54 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=1696962206; 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=vy50Imp7hsFcD9fQ9yun20NmSXPiCxJvkitJven0GZ0=; b=L+KNzVwFf9wmNcf1cAu0VlfDCeBR1oKAWPs0zHdqRlpihQkjaGppLbMeexjVls2tb80chQ SzO1kdXDpTXz1AtIpEabDQLsVnRHkc/hgXJMWs//EpqMDL5kFL69m/T7JefwkQiJnqhfLc UvM4yzIOs1IKH/+YmS5GD9fYc/kjSA8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696962206; a=rsa-sha256; cv=none; b=PxelSxZjI+0U88UB26DOKQRRKoGDyIm88z9WKtB/gTBHhykWXTt2HI0072HNaUOUG9vAGo KqQEB6FRtIVF6YP6QfQiK2GmyhlIaXxmNOzbpiOyeGeDxfx8M/jjrzw37nalzHCnVKCPms XuAs3JW2j8gXe+pzQ7g2THva0/tNbEY= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FmNYxWtb; spf=pass (imf09.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.54 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-40535597f01so56318115e9.3 for ; Tue, 10 Oct 2023 11:23:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696962205; x=1697567005; 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=vy50Imp7hsFcD9fQ9yun20NmSXPiCxJvkitJven0GZ0=; b=FmNYxWtb0x1bKIuep3Lvf9kS76nUTCfVthDvOCtxMw2FbrDMUnBXZhqjy8rSmHpzVi XpZT9g4pKs4XEorXH2uOMH8U0HJERHxG33Ts+qAWJGVKc1zBjlHiv37Dy45veesQafPF OIkl1PmoTNVHMyw7I6qeZnbBQmhqLxsWolifOIrT5vKzLic3sBD2a48j+a5tFv6PfzU7 coxs/9AQCD8581vOoSv+/ygaap8krUKqlDrAPuOpLWztyf0rFzxwCDz0LTd95yHAlnnj lil28o3oKTxRegz6evtxK2GMd5C9kVK8mtRXRHmvRcsNNNtTSZB+yQg7tHDPluz6UGqA p1Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696962205; x=1697567005; 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=vy50Imp7hsFcD9fQ9yun20NmSXPiCxJvkitJven0GZ0=; b=lTAaW0L6aEEqlftYhos974ivWURzJtmLUc2C7nvtfjioJXYfUuNvY7ZDwrJWJHGkub Gck03oQzMyA7GBtKy8PxVvSqCMpdOlJu0nvzDSOqpu9gP4V/DrduUAgIIYM9zqZELUbU 9QZ65avmW5B3zdWKZNsvcZDm+JudDJ1M3xeSjYX+0SVJKPi5hULIeZJeZY7wrZAAFKZm PMpGYM3HWCdmIlT4yVCTC+1Z5xqv5OYyl3OIuE12Fp/b2duYfMAeAXYTrs0Dxp+wbY/J aeGQVChk5vqXN50cdMe6sA2GLZ0OoES7+bOejTEQs3kY1K5CNYZtPmFMULTALEhw/840 UbbA== X-Gm-Message-State: AOJu0YzaLTpPqtMxRxoEe+dNvs1o0MAvBjm8kGy6wBIWNemgo0xbAYf4 7aUfthBK5e/4Cjxe3ZsdkNiHCjTvvvQ= X-Google-Smtp-Source: AGHT+IESkqPmjjAOvbILmnJkd4RsbqA/GSj+EQxsInZHrckbIauEIItGkohtMZgNuXuLytwtz90QZQ== X-Received: by 2002:a05:600c:ad0:b0:406:7029:7cc3 with SMTP id c16-20020a05600c0ad000b0040670297cc3mr15993235wmr.28.1696962204923; Tue, 10 Oct 2023 11:23:24 -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.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 11:23:22 -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 4/5] mm: abstract merge for new VMAs into vma_merge_new_vma() Date: Tue, 10 Oct 2023 19:23:07 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: C07CD140009 X-Rspam-User: X-Stat-Signature: dfj4c77zxdjuuhfm3y95j5mci37uqfuq X-Rspamd-Server: rspam03 X-HE-Tag: 1696962206-711202 X-HE-Meta: U2FsdGVkX19GO9FQuYcIAw+60+clYPLC7TesCcG7czVAn81P1aGXi2ZMS3Aq/dY6dl7BfrvQdAjUN/gdhB5agrAvoyh1vu8VQN2g4l7rNTiduaU+I+TdRdZtIt5PXeure181I8uQzYW7yOE7EsDWd13BIDsiGYClkZSE1Fbu/RhfMW8y5s/jsq4cds/BmdmehBpJgHmRk/t9xEg9ye2kaQus17xk3z60cPlTGxVdMSe9XPiUHyjxH4OFaTkFUpELn5D1xTBXf/2h5KnGtvA/+1yeJOdWppIzGRW5mujrr8WTJMoV09AlA3DyquuwFmrzH/Sp6j/ZkAPbj4pcOxrWEh1jYRztcoTypT0FU3RKsMpdNeky2h9zIL5/PrgNBWwn3QGYUaQqZ99w3dLNTNdylGB/UP8INry50bzPNfa5Z/WhnUZt1q+u40wSppD5Tz9zQZu2EsYd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ity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Only in mmap_region() and copy_vma() do we attempt to merge VMAs which occupy entirely new regions of virtual memory. We can abstract this logic and make the intent of this invocations of it completely explicit, rather than invoking vma_merge() with an inscrutable wall of parameters. This also paves the way for a simplification of the core vma_merge() implementation, as we seek to make it entirely an implementation detail. Note that on mmap_region(), VMA fields are initialised to zero, so we can simply reference these rather than explicitly specifying NULL. Reviewed-by: Vlastimil Babka Signed-off-by: Lorenzo Stoakes --- mm/mmap.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index a516f2412f79..db3842601a88 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2485,6 +2485,22 @@ struct vm_area_struct *vma_modify(struct vma_iterator *vmi, return vma; } +/* + * Attempt to merge a newly mapped VMA with those adjacent to it. The caller + * must ensure that [start, end) does not overlap any existing VMA. + */ +static struct vm_area_struct *vma_merge_new_vma(struct vma_iterator *vmi, + struct vm_area_struct *prev, + struct vm_area_struct *vma, + unsigned long start, + unsigned long end, + pgoff_t pgoff) +{ + return vma_merge(vmi, vma->vm_mm, prev, start, end, 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 @@ -2840,10 +2856,9 @@ unsigned long mmap_region(struct file *file, unsigned long addr, * vma again as we may succeed this time. */ if (unlikely(vm_flags != vma->vm_flags && prev)) { - merge = vma_merge(&vmi, mm, prev, vma->vm_start, - vma->vm_end, vma->vm_flags, NULL, - vma->vm_file, vma->vm_pgoff, NULL, - NULL_VM_UFFD_CTX, NULL); + merge = vma_merge_new_vma(&vmi, prev, vma, + vma->vm_start, vma->vm_end, + pgoff); if (merge) { /* * ->mmap() can change vma->vm_file and fput @@ -3385,9 +3400,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, if (new_vma && new_vma->vm_start < addr + len) return NULL; /* should never get here */ - new_vma = vma_merge(&vmi, mm, prev, addr, addr + len, vma->vm_flags, - vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), - vma->vm_userfaultfd_ctx, anon_vma_name(vma)); + new_vma = vma_merge_new_vma(&vmi, prev, vma, addr, addr + len, pgoff); if (new_vma) { /* * Source vma may have been merged into new_vma