Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp614316rdg; Tue, 10 Oct 2023 23:34:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGWg883fQr27WvKhNxDGUf173uyyMwNrkFkoRCSKxdd+2groqt5l2GYj4DKvWsgV3s2xtBQ X-Received: by 2002:a05:6a20:72a2:b0:15e:bf2b:e6d3 with SMTP id o34-20020a056a2072a200b0015ebf2be6d3mr22518854pzk.46.1697006098177; Tue, 10 Oct 2023 23:34:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697006098; cv=none; d=google.com; s=arc-20160816; b=T3QEn8pnXUKhrLd1urzs/geJgSVHPJQqF/LDtlclenaQF4O9E+mQoUh6Sj8vyzJ9Hq TaRfmJHYZYVxlo940Xve93wWJo2jDEPVeJHMJ5cKeVG3/3WHw3DeGBVZGgSrWpNo8NHp 8gmsNC4qm2VuscE92tJiHDq6PcM+bobo3Ava/lwLpTRGSafAfEaMd2CEQAOQ+sm3//yd scu3CcTfvK/aZrtna75mE752DshJjJYLvcfUXI3LVNMuVOHoIWc/LmWQ8W0ZLFQFgNCI SdMipbj84xwOhqHT4RgBf4kRxwaYREt1fRkaBQKmdCJcokyfnpkpXp36zyQGKkyZoiUE VEyQ== 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:to:from:date:dkim-signature; bh=VEaM7V4d54AdkDOOySpRi+Dcj4bMPN5Z5/1QUsaZsRY=; fh=F06x6r7uWUi4GgxqKbw+xKsG51SbqtTYeqWTbOMrHhA=; b=MyouAkwgnHLrPmYQsvNfELoKVCe7R/8rCMLiNuPlv6XnQJ/78do8CgCHmLbgKzvmyg W17TwAFjcmiRAk3O0i8YltTI6KUiDbMBF8AaHkQRPuLxldG04OBnOtZ9f0GIbH/iGppK T9JN3AVXk+OSgckqbPUrXcURX9Yb4521J6O/6tpOSTG1kPYqzrKtVetzNQSwkxmd/lq1 vpvy2DE+UDloJkXAhBUA5114Y/OfpIv6YmjU73Gnchlm1G+eBHHJH3DpUhbZcZWPx0A+ XJET3PFU9q57+kLlxit4WhNR0pOkZs2NRaWepLM9yV6vLI4F46V7DhYdGzjnacA2Dx+G CZgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=H3Jo4LEM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id i13-20020a17090332cd00b001c7249f5e40si14155124plr.469.2023.10.10.23.34.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 23:34:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=H3Jo4LEM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 04A6281890A9; Tue, 10 Oct 2023 23:34:57 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345234AbjJKGey (ORCPT + 99 others); Wed, 11 Oct 2023 02:34:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344555AbjJKGex (ORCPT ); Wed, 11 Oct 2023 02:34:53 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75A8198; Tue, 10 Oct 2023 23:34:51 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-32d834ec222so483859f8f.0; Tue, 10 Oct 2023 23:34:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697006090; x=1697610890; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=VEaM7V4d54AdkDOOySpRi+Dcj4bMPN5Z5/1QUsaZsRY=; b=H3Jo4LEMNSH1fGPa07lgApMEzhxDSe92tyJ1hRtTFvzMV9XqhKLdsbgAfRl47n+Myq MAwUF109eIz+2A8q/WDqX7cWqZoZEemGT7n6vRBmbhR9Le8uWXXRW6qwfbE89YDgohPh GtBf7iTtPNTAIqjueZB6+hkov9f8QFjZ09ikdsgN8NIe3VmLNwk5hDrKspYgV0uhkm/P I0wK9vfyztvOf+KK1H8qRWo+5G7T5+ZoQGx5X5DlEI+W9aDqhiAgO+UfcFe0h3xLnv1D f0dJH+Gudpk/tTDetR0fAqkahSytdzy4t/Gt31krYk9yNy+RYUVSo3gbvryYpYJKI6zl plWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697006090; x=1697610890; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=VEaM7V4d54AdkDOOySpRi+Dcj4bMPN5Z5/1QUsaZsRY=; b=JcF+558IkgsCHzu6wYYbqKiKRa0zbm3zEIuzU2Vy5ArA6rQsPKOZmNw7nSa9GwXoVz kDVmIb2bAf+p1bgxwiizf9/mTE+lPzLC4O34ZRNTahTKLkbLAI05cPZwPeEl8+099T73 8+r2QIJAghfJggALZZumOIYKZ4IW+rv6bMBJEFoXnsRiJ2otoUGJXwO6XexildSkcAfP 9yW9GcR2zqaQqQiWD+CIyZgFGSS/FNzpiQYxw9GCLmQ2ONXAd9n/Z6SDPE56Qcdpk+NZ jgCMDTLJ6g6JK2UnIyG/BWt8Tf/Zl58yZ+fTS/AVTPNxviUPYsJ+5vFf6AtqwBe5uPJd IXoQ== X-Gm-Message-State: AOJu0YzApON+LxWdg2Z5xEBgkuNxFbG4NuUWsfRhqIRATxUMDO/vXzmZ uPcamjjl+M8FR7FLGBE99Qk= X-Received: by 2002:a5d:5b1b:0:b0:329:6e92:8d73 with SMTP id bx27-20020a5d5b1b000000b003296e928d73mr13728679wrb.67.1697006089625; Tue, 10 Oct 2023 23:34:49 -0700 (PDT) Received: from localhost ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.gmail.com with ESMTPSA id a12-20020adff7cc000000b0031423a8f4f7sm14494828wrq.56.2023.10.10.23.34.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 23:34:48 -0700 (PDT) Date: Wed, 11 Oct 2023 07:34:47 +0100 From: Lorenzo Stoakes To: "Liam R. Howlett" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Alexander Viro , Christian Brauner , Vlastimil Babka , linux-fsdevel@vger.kernel.org Subject: Re: [PATCH v2 2/5] mm: abstract the vma_merge()/split_vma() pattern for mprotect() et al. Message-ID: <02957e4c-6752-4039-bcc5-c00b971ad0aa@lucifer.local> References: <20231011021452.57vhftchkfxebppe@revolver> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231011021452.57vhftchkfxebppe@revolver> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Tue, 10 Oct 2023 23:34:57 -0700 (PDT) On Tue, Oct 10, 2023 at 10:14:52PM -0400, Liam R. Howlett wrote: > * Lorenzo Stoakes [231009 16:53]: > > mprotect() and other functions which change VMA parameters over a range > > each employ a pattern of:- > > > > 1. Attempt to merge the range with adjacent VMAs. > > 2. If this fails, and the range spans a subset of the VMA, split it > > accordingly. > > > > This is open-coded and duplicated in each case. Also in each case most of > > the parameters passed to vma_merge() remain the same. > > > > Create a new function, vma_modify(), which abstracts this operation, > > accepting only those parameters which can be changed. > > > > To avoid the mess of invoking each function call with unnecessary > > parameters, create inline wrapper functions for each of the modify > > operations, parameterised only by what is required to perform the action. > > > > Note that the userfaultfd_release() case works even though it does not > > split VMAs - since start is set to vma->vm_start and end is set to > > vma->vm_end, the split logic does not trigger. > > > > In addition, since we calculate pgoff to be equal to vma->vm_pgoff + (start > > - vma->vm_start) >> PAGE_SHIFT, and start - vma->vm_start will be 0 in this > > instance, this invocation will remain unchanged. > > > > Signed-off-by: Lorenzo Stoakes > > --- > > fs/userfaultfd.c | 69 +++++++++++++++------------------------------- > > include/linux/mm.h | 60 ++++++++++++++++++++++++++++++++++++++++ > > mm/madvise.c | 32 ++++++--------------- > > mm/mempolicy.c | 22 +++------------ > > mm/mlock.c | 27 +++++------------- > > mm/mmap.c | 45 ++++++++++++++++++++++++++++++ > > mm/mprotect.c | 35 +++++++---------------- > > 7 files changed, 157 insertions(+), 133 deletions(-) > > > > diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c > > index a7c6ef764e63..ba44a67a0a34 100644 > > --- a/fs/userfaultfd.c > > +++ b/fs/userfaultfd.c > > @@ -927,11 +927,10 @@ static int userfaultfd_release(struct inode *inode, struct file *file) > > continue; > > } > > new_flags = vma->vm_flags & ~__VM_UFFD_FLAGS; > > - prev = vma_merge(&vmi, mm, prev, vma->vm_start, vma->vm_end, > > - new_flags, vma->anon_vma, > > - vma->vm_file, vma->vm_pgoff, > > - vma_policy(vma), > > - NULL_VM_UFFD_CTX, anon_vma_name(vma)); > > + prev = vma_modify_flags_uffd(&vmi, prev, vma, vma->vm_start, > > + vma->vm_end, new_flags, > > + NULL_VM_UFFD_CTX); > > + > > if (prev) { > > vma = prev; > > } else { > > @@ -1331,7 +1330,6 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx, > > unsigned long start, end, vma_end; > > struct vma_iterator vmi; > > bool wp_async = userfaultfd_wp_async_ctx(ctx); > > - pgoff_t pgoff; > > > > user_uffdio_register = (struct uffdio_register __user *) arg; > > > > @@ -1484,28 +1482,17 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx, > > vma_end = min(end, vma->vm_end); > > > > new_flags = (vma->vm_flags & ~__VM_UFFD_FLAGS) | vm_flags; > > - pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); > > - prev = vma_merge(&vmi, mm, prev, start, vma_end, new_flags, > > - vma->anon_vma, vma->vm_file, pgoff, > > - vma_policy(vma), > > - ((struct vm_userfaultfd_ctx){ ctx }), > > - anon_vma_name(vma)); > > - if (prev) { > > - /* vma_merge() invalidated the mas */ > > - vma = prev; > > - goto next; > > - } > > - if (vma->vm_start < start) { > > - ret = split_vma(&vmi, vma, start, 1); > > - if (ret) > > - break; > > - } > > - if (vma->vm_end > end) { > > - ret = split_vma(&vmi, vma, end, 0); > > - if (ret) > > - break; > > + prev = vma_modify_flags_uffd(&vmi, prev, vma, start, vma_end, > > + new_flags, > > + (struct vm_userfaultfd_ctx){ctx}); > > + if (IS_ERR(prev)) { > > + ret = PTR_ERR(prev); > > + break; > > } > > - next: > > + > > + if (prev) > > + vma = prev; /* vma_merge() invalidated the mas */ > > This is a stale comment. The maple state is in the vma iterator, which > is passed through. I missed this on the vma iterator conversion. Ack, this was coincidentally removed in v3 so this is already resolved.