Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp621734rdg; Tue, 10 Oct 2023 23:58:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH5Ghyb+VBNszH485xilFfcr/NspUYVJrJllYAlUBu0IJtEnAtH3tIgV6mtjkKAaR5fHQte X-Received: by 2002:a05:6a20:a11a:b0:13d:ee19:7727 with SMTP id q26-20020a056a20a11a00b0013dee197727mr20359070pzk.18.1697007522417; Tue, 10 Oct 2023 23:58:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697007522; cv=none; d=google.com; s=arc-20160816; b=pVMu45Rjl26z2IRhFjIBmjWkYGw7n0FaEAQDjNCYhhGyuWohZQH368h+XdpOmJm0cL sxINhrcI8H4OqG+vgbcyWcg9C3Zm4X5tL0dId1bi9d3kPEaJbOvvBE+mskTPlU6dWXa4 zs+LFJX9STUMQ+4wgqjapeuLX6de/3aM4i62DwVTs0lMbsWpeXiCN2oMIighManfS1CI Pd8fb1E67Pfl6Q9vOkW0Ax8OF+g7V/bJHDtYb0zLaISqm8o8JH6x+tIORqUgBSZFrLtK pSw7S+ifT7NDknArxeWmQ3N5WMa5xSsCxzaA6Ym+psODpURwEMadZqHaEvbgcXwCagMs 829g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=94Aupc/WoctqDp8xqPo3RjM/d0ECIJ8ZPHye9gY433w=; fh=IYsw5cHXYa/Vt1r8QmplANdC3OwOXLr4neZVlSxTk3w=; b=z2Hg+fYuu0q4wPMoQLdNUC1mil64piuPISbDD+MajQ/VoAN3Rj8dXswng1jwf9pIuh 407277/dZicycOxx4A5ZlUSGh2GawRP2YabrM592ZqMBf0uSgjgLSEtShEltAF3Uuf36 w6HhS9fpWy/Fw35BcswIT2lKUDyseEv0CRKRX4gGXPmOTGGYWcZs/oIPneeUBSlD2niA KApadjttN11EStwSFuGgTeVIWf8iN1A13t12Z67Ng77P6CoNLevFGs4efwktMVe82YqM jOKgFqRqID1Zf5Cmxm2fUt2JhuIdYE9Z5/q8i6zJg8ZC5coOQb/PqbrbI6JCTY+l6OkC sZXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=KIxB5r+j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id w20-20020a63af14000000b00573fc71f6d6si13700392pge.6.2023.10.10.23.58.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 23:58:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=KIxB5r+j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id AB46281049B2; Tue, 10 Oct 2023 23:58:39 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344185AbjJKG61 (ORCPT + 99 others); Wed, 11 Oct 2023 02:58:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59020 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229587AbjJKG60 (ORCPT ); Wed, 11 Oct 2023 02:58:26 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79E7DB7; Tue, 10 Oct 2023 23:58:24 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-327be5fe4beso5874113f8f.3; Tue, 10 Oct 2023 23:58:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697007502; x=1697612302; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=94Aupc/WoctqDp8xqPo3RjM/d0ECIJ8ZPHye9gY433w=; b=KIxB5r+jsya9wKwRiOtdw3I/3ZxD82m8uL4DwQRBz/PS8KGJ4FyFsufmprI6OBVu9K pR4Iay02IcbgkeHLid+mfNrQN84FFpOgclACZwjIwJMkSuxnV9UfV5q5hXYnhhp5uye1 Hg26/vz5dROysidFxa1WVBO7kZHgoM/TdDtCW8Zgkg90YQ2rcmqYC/XwKPSuMH3q7VDf rVB2MuSxhufs2u2iLpFASzPsG6x8SZfm07Abv9qTQ7TVQAcHWF/HVYbazydoRY8izVvC rDk67qq5LM/9AAHrvar2tNg9eeWiC9x3MUeqw+ziY66Xq5iyGjIILJg48ZCvtGcbqiA9 FfWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697007502; x=1697612302; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=94Aupc/WoctqDp8xqPo3RjM/d0ECIJ8ZPHye9gY433w=; b=l0VQtUiyaHVKsoKC7Ng9LS/ABqcWfu+7DGIbyIpIcz0roNOVNkbwObt4mt37RszTEd Pk18jlJdYndTYxzj+wEPysgczdW5LTbf0OnSwydqy3iSMzcEBKsxwMDIz7Tdr0qCxaQS t9qw1wP6vQrKZL5ErpXBHro+1oqGD7cVlwdvonrEk7Ps4ztAfeT7LGgqNNOkH9WXNsyP RkRzfYZsteFMeK+lmZEheQhO+L5OzwaJrPHLQpg1GE3Qio1Bk6ZzbctBh1DZMGb2RXs/ VtCZ2Sx7yZ/YSG8IdrYtGD5wVERS7sRGQ82cb7yQtRPCYA6qaYSucJwgfyQgVHo5XWS6 GP6Q== X-Gm-Message-State: AOJu0Yx19uMRGv5IbmRvjXexba0g0Ha6VoorgolkUPr5yvfglAtf5gPs MkR3kW11J53IgumzZOgb7jgJkvDTGLI= X-Received: by 2002:a5d:5304:0:b0:319:6d03:13ae with SMTP id e4-20020a5d5304000000b003196d0313aemr16290191wrv.55.1697007502350; Tue, 10 Oct 2023 23:58:22 -0700 (PDT) Received: from localhost ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.gmail.com with ESMTPSA id p13-20020a5d68cd000000b0031ae8d86af4sm14469751wrw.103.2023.10.10.23.58.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 23:58:20 -0700 (PDT) Date: Wed, 11 Oct 2023 07:58:20 +0100 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 Subject: Re: [PATCH v3 4/5] mm: abstract merge for new VMAs into vma_merge_new_vma() Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=3.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_SBL_CSS, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 10 Oct 2023 23:58:39 -0700 (PDT) X-Spam-Level: ** On Tue, Oct 10, 2023 at 07:23:07PM +0100, Lorenzo Stoakes wrote: > 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. Andrew - based on feedback from Liam on the v2 version of this patch, could we change this commit message to:- 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. The VMA merge call in mmap_region() occurs only for file-backed mappings, where each of the parameters previously specified as NULL are defaulted to NULL in vma_init() (called by vm_area_alloc()). This matches the previous behaviour of specifying NULL for a number of fields, however note that prior to this call we pass the VMA to the file system driver via call_mmap(), which may in theory adjust fields that we pass in to vma_merge_new_vma(). Therefore we actually resolve an oversight here by allowing for the fact that the driver may have done this. > > Reviewed-by: Vlastimil Babka > Signed-off-by: Lorenzo Stoakes > --- > mm/mmap.c | 27 ++++++++++++++++++++------- > 1 file changed, 20 insertions(+), 7 deletions(-) > Thanks! > 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 > -- > 2.42.0 >