From patchwork Sun Oct 8 20:23:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13412797 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 54360E95A82 for ; Sun, 8 Oct 2023 20:23:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C88716B0168; Sun, 8 Oct 2023 16:23:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C3A326B016B; Sun, 8 Oct 2023 16:23:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AD8E96B017C; Sun, 8 Oct 2023 16:23:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 97D956B0168 for ; Sun, 8 Oct 2023 16:23:28 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 67D68C01D0 for ; Sun, 8 Oct 2023 20:23:28 +0000 (UTC) X-FDA: 81323419296.23.C4FB5B8 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) by imf02.hostedemail.com (Postfix) with ESMTP id 872A280004 for ; Sun, 8 Oct 2023 20:23:26 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=THgLQaTr; spf=pass (imf02.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.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=1696796606; 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=GphpI5imAra4MlTQiZu1du23bwIkT93dz+Vpq/IpRZ4=; b=zWytBOMtFMp+/Ls4Kv6C9LWXNN0w5vq+rKfOFHU1vZ6NxHlTKE4Xehm40tcEx69ePpmRXw Q9g5/jRkmd4+PpZTElL7fsR1kife/M3WM8MtS1xsBJ9scCQKq65RevTKut13b+iIYnk4Su 8bqo5hYzbkIZGTGvDFJSgd52YZzgDh0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696796606; a=rsa-sha256; cv=none; b=WEb6OSPwqOXoZ2TeFxWXXL+4D+rROMEBjijz+Oy1ZTxueTfdxKSIBViXZEfoNDRr1an0QC dM7Wf0vnvujInGk3OtiCJBUfe6W1KfFbudx2qpMRK9uEz4+EB7RGSDXYIPwJ2ieHk5canZ NWAj4Fg39GoG/qBn7RjVx292DtPU81A= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=THgLQaTr; spf=pass (imf02.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.52 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-406609df1a6so37137745e9.3 for ; Sun, 08 Oct 2023 13:23:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696796605; x=1697401405; 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=GphpI5imAra4MlTQiZu1du23bwIkT93dz+Vpq/IpRZ4=; b=THgLQaTrSvv9CiQ3jKT9saZYIuMhwVUj86p+mB8gfQsUk9KXId2gD2ExtWOd3jtvg+ MFiNFF3yB9QoR5v/XS3ZtYAdk387PErAF5gXiXne4o2EuGDlJpmxOz61VU8uctDDBMHx QsoeZh2j/CmvKtebHN1IqZiLNMAU3NxXEotM41g/HJNB+qq3uTeHddGNZHmMmF7M3h7d pFOfwBJTKDNXiOmKsM4tJVTPBglvZa0swqevIIJHqmpudNDKHv2qBn3gdit02mrvMrny 5MF7pBaqso4oZdIrqLZ/MWMTfL276AK9pahTwHlVNnN4fMPC9wA9IKAYx47yedWh8LBS XLSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696796605; x=1697401405; 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=GphpI5imAra4MlTQiZu1du23bwIkT93dz+Vpq/IpRZ4=; b=VtjJIaz8wFnQb8gAIdvlGsQcqioj14aNwrA8uZlb4WH3g6P0FiIbulUDLBGZLNcZ1S hqiRmCM/1OMxiOTp9oO58jw8a1mRjHRodOoYkkYYdSkQudXb+xXn+8OyYpI3laC4eKUh mArHrOYSZ7itiMkELbh96ueoqHVHXxHV/cQ0IfnLOeA3Ge5O7RL0V64/FS6smSmv37mU cAv5OComg22FULwvEbFhcPAPAZfPNtlLBbFSO//QsfmS9YUVMcY8NbZOIn3nWkCUqJG1 Pf2fv55nMExrsrzAGR+HRn01k39D2icL9coFGht0cQEjkVV/xlMy/gqh4its8B3FnB4w 265A== X-Gm-Message-State: AOJu0YxkNupPxWbUP4+0dfPcRnIydfg/4R0uXejuT2Yi2KGHbJ9SIExf q9zG371dT2/QfCOK4Ew6RVw7LAIEt0I= X-Google-Smtp-Source: AGHT+IFLzkXje9IEvfeVChyj/N8dc9zcG9gS5hukGXOpPqdRcGECy0kYVxH9DGQPH7cdYFKyB/CDNA== X-Received: by 2002:a05:600c:204f:b0:3fe:3004:1ffd with SMTP id p15-20020a05600c204f00b003fe30041ffdmr12585263wmg.4.1696796604593; Sun, 08 Oct 2023 13:23:24 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id c5-20020a05600c0ac500b0040586360a36sm11474879wmr.17.2023.10.08.13.23.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Oct 2023 13:23:23 -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 3/4] mm: abstract merge for new VMAs into vma_merge_new_vma() Date: Sun, 8 Oct 2023 21:23:15 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 872A280004 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: fau5kzwaz7cjs81zffnqksb4ouw1yes3 X-HE-Tag: 1696796606-524270 X-HE-Meta: U2FsdGVkX194TR7daTi6PqEacrOu4M3mR37w51wVYZMpI3VFPZhKZNnun6UAjGf6l2pFTrho+I0UEPb51xtApXRSeoZkfO6rtQxyELttmmsZRJGg2gzzGs3zP04O5iuwPkzLGkV4pbS3ggzMr8nEhW6S1kUQmJ5VB+C48HY1pgjexvgtUpbIBCqPUhDoBLIvGVottD3+5fmLiYmDYqlsEDi0jZ0trYSf7VuC064EW8hSxP3ach2ufiGNrDYqi5OyHO1PMlBDhiWICOpXgtj7x2fkPdlpxVxNtaACDzdCdM1A+nf0gTyrEHuPRzth1T8BPODV70WGEsr0BJjN2vulqaWhrZqwdBkJ82Vwc7vtGJvV0yaJQBm822V8idyPkD25LVCEhZao6dF8Z6eFCNDhcJzr21dhlbSMlyqNUUGt5BS/tbd3EmN9J0HIvzRrDbkAbU4CPfzm8+4RECUNR2517nYeUNW6JRM8hWij+UdO66g0tE6a2+ky2H+W8PXkQkPGfaIqlVyqo3q/x1tR7ERoh715Vsldb1GMS1YKNIM/Jw+NWuCQmmiof10MOVJLKBX6DSN2g/KMuMatHbvbJPx6U4Vdn1wuo6kxHT+JxgOk2xqET7HyMOThF3XslMqUmzpiPyO75klFjioe1fmPYh+wUqFX09H0rKhZRqvJO6eEDTbR4MnfbYfwhfXz0x90m7blUdZcIULn3md0ZY5DggBFzpaZHwqJVpfeLEqLDCV/s3PgO5ciiJBd3wr5i0eZnNmVWipvKWACYVAc48gZz3Caq6GVP3OpvXgtvy6xgph8yWL6yoY/ymNVlE/krSCKJTFM4JLODShATfvQNg8BlLC4S7tLqaiSneQP+TubA9SUdQDSiyxlHgDiQhjPu/gtX/v1dslwdIpbf32S1UK5RzuRPdTLyI4d3gdFJ4FaezwwOQwA/6awhq5LVQXrb16wnI4jSmy9T7uFEoCSq6S3zZg ssoF9QyH 1xsnBEoftyHj1gxkNuOH1lKySPM1Fa6GAZkcBKECdZ/i/p7qjUW0X2RUvGLkEva/Nv6rrIjnAJViMU2FiumuOOEtCZF5+q4/a2pivZ59Mao/whmAzE7xeGCQKM6IyqYp2KacSceheh011UpLDfWd/3MF2IfGam4Jew6TSvgkBDuEFBwStCft63W6tvjCIM/YDwHe8kU1w9/di83E/RoTETp9sWnYfRl1F4iG0GoNsOsaA8JHA+t5FgXNUC9dwo4H85ssdoqKoAsj9gA9GYM+zNKrsFnUsYo66ruBHBJjGACYe2VJ03IZlR2eBs2PVymO2dnJhAJfl2VJ/i+GhBvp+o1feYf2yTAsoJaTyfZLfBuM3xD5skxfq6T7WP1db+ffxv31Hr98+XYxsf6cf5dYkaVCr4TE8lVEitJp0gzMFo1nX8EmRBlLAW38HSP0veSNgAa1RXeGLg3fuKV+O4lSPTxwMOxqOPemz0PpL/ozqVXaeQtsjK+h8BSU4npiYzg0w19CF 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: Only in mmap_region() and copy_vma() do we add VMAs which occupy entirely new regions of virtual memory. We can share the logic between these invocations and make it absolutely explici to reduce confusion around the rather inscrutible parameters possessed by vma_merge(). This also paves the way for a simplification of the core vma_merge() implementation, as we seek to make the function 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. Signed-off-by: Lorenzo Stoakes Reviewed-by: Vlastimil Babka --- mm/mmap.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 58d71f84e917..51be864b876b 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2530,6 +2530,22 @@ struct vm_area_struct *vma_modify_uffd(struct vma_iterator *vmi, vma_policy(vma), new_ctx, anon_vma_name(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 @@ -2885,10 +2901,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 @@ -3430,9 +3445,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