From patchwork Mon Oct 9 20:53:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13414432 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 42ACDCD6136 for ; Mon, 9 Oct 2023 20:53:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D019A8D0091; Mon, 9 Oct 2023 16:53:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C62668D0089; Mon, 9 Oct 2023 16:53:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AB4A88D0091; Mon, 9 Oct 2023 16:53:42 -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 942548D0089 for ; Mon, 9 Oct 2023 16:53:42 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 650C0B4B20 for ; Mon, 9 Oct 2023 20:53:42 +0000 (UTC) X-FDA: 81327124284.23.9F010F0 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) by imf19.hostedemail.com (Postfix) with ESMTP id 699F51A0004 for ; Mon, 9 Oct 2023 20:53:40 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=l0SYogZ9; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.42 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=1696884820; 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=peKTa5JCQROacC2rkKD/EMjKt2/PWD6RJuctYYclWYk=; b=BXEWHN+x+7YQCfalGJ+SDLJu2lj/GoeNIq8YQsuCxt7NjhlvFWJzvJUZ2653bo5E5nKpIf 1o5gNi2mxs9834w7tE8sL/euf/OUlKveUSN2MFGsvs454s8zbjmYcjvsAeJpEY2Ly5CJQU M7zIR/8HJpKLunK4rB9dcfLQEOzv5oE= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=l0SYogZ9; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.42 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696884820; a=rsa-sha256; cv=none; b=vAdOEMjGl57eJxWbpPt/GnH7JaQoAJKnX/Ph99CpIJieVpT3vXDt5AmPUvwkFz5v+MZfEr p4oJk5mnDlScUVjhdHpvqtiQS50LbesvtANED36g3sw8bhN05BlChIBK9NS1ceE/FZ4oO0 5KC4Gamxjo5T1y/cR3epiV06Eb7EyRk= Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-405524e6768so48373485e9.2 for ; Mon, 09 Oct 2023 13:53:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696884819; x=1697489619; 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=peKTa5JCQROacC2rkKD/EMjKt2/PWD6RJuctYYclWYk=; b=l0SYogZ9nlka6YqCNtDpXmHIaib3U4wzj3GYln7IG9z5XQnfevl0VYZvKQ8a1ZpDO6 pWOAeVAd9VyOWCdI2UVRBL69Rf0cEBcd7ZhOV98DmwbnbG1bay5VJdvl3NmgXKPInb3C ywOlW/6R2NY0llltwqKCQ3nWQQaHOTzpwnFd/bGOseBJ8QMmIbGqHoPMk+zZmr6DbmIr zObcnoWvSB5IIHagKUhAaeJ2mu4R62MC3Es9in1Xl3B15ysAcB60pBb7egMjnKo9LABD r0SiuWmo6yjqv1hLCYoKZeNhHtg9eypK6zgIQ9tTLksxPEu3ZlpUJDzDc5Fwb09hHVcu EH5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696884819; x=1697489619; 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=peKTa5JCQROacC2rkKD/EMjKt2/PWD6RJuctYYclWYk=; b=R8Fm7pv67eISKwBr5ojT3XI3LWRTCdnmQZ80RO7WT9tqm5TtOy9bFv12OJKYLpb/9w mJALLiFoRJY7hytQExrbKleHvEHYCXZitZgknIAVBYl2CwtdNfmbLejhda5TqbJs5R8L aDssfUbMrDHpRXXEm2caJg6W6CrXyCgKt+1VS9ldfce/Yukx2oo4AU9lS24yqhkIjDQX uz0Es1FOdPxcqCKati81XJq7gwMUi2Vw5BPItSr2G5BJHWcOZvkdDzNAl/0a0pABfls1 3O/f0i3ycnuHB5zCqT9UXmByn6twr9QwNAI6MZX0s5hZd6pQNhGITF9i7FxXIJ+J7JMd BfxQ== X-Gm-Message-State: AOJu0YyM+bbgxaWHIuXfdBCzhky6WN6ZG+x6qD+7Os8hjTR7CGbgTmqe 7EgALGZb3EEqdTeLgrkOFbti6IrLQm0= X-Google-Smtp-Source: AGHT+IECOr/NuIGQT0xwu1GNnQ3mzgM7UNNwWg6rN1KgZwpIM+meh3t/4/xhAlA92IoCFh4rHNSa8g== X-Received: by 2002:adf:a4c1:0:b0:32c:a9ac:2bc1 with SMTP id h1-20020adfa4c1000000b0032ca9ac2bc1mr216695wrb.63.1696884818605; Mon, 09 Oct 2023 13:53:38 -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.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 13:53:37 -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 4/5] mm: abstract merge for new VMAs into vma_merge_new_vma() Date: Mon, 9 Oct 2023 21:53:19 +0100 Message-ID: <8525290591267805ffabf8a31b53f0290a6a4276.1696884493.git.lstoakes@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 918gy8gpf7u8o5rchrht3amtdyb8rpmu X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 699F51A0004 X-HE-Tag: 1696884820-754089 X-HE-Meta: U2FsdGVkX1/A2XV/A3Ea5PMiTniBpMcW7/ZzAsHR7cSOztEn/6jFjFRy42hmDGTist1ou9k5iDcgrxol5WHGvssDHaktYgXhn56OxQL3mqOifCYNEyd5H40UiM4X8IlbpyUzGi98U+m1F0Mdmgl8M+TTXIdPvpXjwPnDWL/tJIUBmQDiJ4iN+XSWtwqE3Vam0frI2CcjtjaumFTvriI3fwyjihAiffCsienLCO1nIvr7uXVIuj5TrzLeKGI9F4yfhUDLoBkQKM1so002NpDwGLKWi01kdG2gIbNO8VYzyhYRfxVebVdi3EEHIzk+p3WlW1040+8CZoxD19wyLESBWoQwrafVjfX7fYu/SyXNGtVhIHXUZvfPv8ZrrIxmHWiN3/bS1QR8m2biyWFnIu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NM6W18O3rX9G3X07sQTezK9 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 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 17c0dcfb1527..33aafd23823b 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2482,6 +2482,22 @@ struct vm_area_struct *vma_modify(struct vma_iterator *vmi, return NULL; } +/* + * 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 @@ -2837,10 +2853,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 @@ -3382,9 +3397,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