Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp1554318rdb; Sun, 8 Oct 2023 13:23:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE9J7LlsVbF18K0f8yKIl7J1JlTRMECTsFg1bRPPmLnJCcuFAy524YPcDJlaVTXmxtM/6N5 X-Received: by 2002:a05:6808:10d5:b0:3ae:156f:d31a with SMTP id s21-20020a05680810d500b003ae156fd31amr18400916ois.44.1696796620440; Sun, 08 Oct 2023 13:23:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696796620; cv=none; d=google.com; s=arc-20160816; b=SRI6w/qpvEMX1MPy7IFsSxMwI2FeqqrxLcl/OEBFCyVXVHlu3xAf+38vz+l/LpOM7t HgjDZM9ZScAtt4ct0jWBdhWe9gDUwQZsqxYwIh+8an/91PXWOon+kSOjR38FJFfRBIUf 2/k0BGR8onV6xZKq4mSjNzAFp8txvSb/61E4mXcwkNy7ci9B81ZlF3r5vmURj+od3v3E 0CPWhyLYXQxZogi0IxNNnnbEtBadMTo37pkAoay1/drUE2x3THb2kXHlR6iDXlEqCjdn X1u7i4w/0iHW6HMkE7zjx0fmjcOJLw0D7vpAyxE2vYFinX05zHrHNM3wuPZivOQTmFH1 j5NA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=oAlFt19XSPitoEhPZtTBItR7ZhRoBTV71rlHM3x983M=; fh=t0e8hB1yztay3y0yczBzLM0YwGerH6s+dnvwo2ZuqJs=; b=VeGaif3qUAECiWsBuCDbxIg/VDtAaH8hnW2gZWzC10UarIEIPm84BSUcyrJd1o1WzN cKJc4o6BKJiL7fyR0x5YOywBoBEdK/RLzTWbqtOAFdW2bqKGGHU3EwzLjgRYA5pAc+aS XIYX/MQXGsIMHZbfddLKQM2PUQggxCwuNrb7HJm3YypDEx20xJBFJKaBfjumuKTt5Ilg jMm5y3J7Lp5fAnj2cFjDlX7V05dlc1K/8MGHlDUAn05mxWv7Nw0VB4Pk0YBB6QsOjhZX 3qHHsuOYOfAcV0OV4KJ03B3uTJeMfVqE+j8p8cq9gimZYU4WoS/BZ1GkLUgrFGOzdNsC 6NmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=hGqkYvHP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id a19-20020a056a000c9300b0068fbac77dd0si6404877pfv.226.2023.10.08.13.23.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Oct 2023 13:23:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=hGqkYvHP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id 57774807E7C4; Sun, 8 Oct 2023 13:23:36 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344552AbjJHUXX (ORCPT + 99 others); Sun, 8 Oct 2023 16:23:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344467AbjJHUXW (ORCPT ); Sun, 8 Oct 2023 16:23:22 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F64F99; Sun, 8 Oct 2023 13:23:21 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-40566f8a093so35599125e9.3; Sun, 08 Oct 2023 13:23:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696796600; x=1697401400; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=oAlFt19XSPitoEhPZtTBItR7ZhRoBTV71rlHM3x983M=; b=hGqkYvHPDPFWQwn9y6Ifhzej8Fv/3iDcbFBKhvSV9C0sTgYibov9KiMW2KzmexCDO+ KyPwRn096MXlm9QHdz/pqKzq4azxxQ665HmNuKC1WH7tMHlyQGlVitmQLbNh6GZYHEdU STVyZqychOSZIIn3YxsySFd8jZpdVQfhWH243Uhhkmo2IEvYCWR9n3Sp4UbdH2cqoBGE uf8LaK4SnjB1IU5aFDuY0sCXL/pAbBfOBPiLhNFJZ7QRO2WlJMvF6A7ZC7SPtAIDwDDs iZkvv3VudK3xY3uXnIQa2Jgf9iNWPYQ/KHKgvI3IeRV02Nzbzl2gkb2RT4Djo/fzu62k 5OUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696796600; x=1697401400; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=oAlFt19XSPitoEhPZtTBItR7ZhRoBTV71rlHM3x983M=; b=jsoRD7+RjPBYJCSuFATsU4CbY2d4EBE5fGfKUBmj27g07c+4RcDfKFI/r2zmP2T6BP IF8Zv4BriShpRz4I18SduRbrDmeWmounJgoUwuaJXgaIU7gMxQiISgQTXhOuKiAbo3Y2 g2qGe6oVurFAkjJmAJC+bqcxKNdDneD1gq7r4BR5yDYJQ9iDLRoKIuIolZj25C1E8KrS O4YDgSsSs30eqr+1niDZCWJ3DuZyumtPv3QTVAGXoYeTCmBAy/7L+7ogBe6ugq2NxVZ2 9rRf5ky/MIEw7tpMv7sZLhcCORp6JTyGeeeSbVu+RlNn20w9JZH+jDKpr1MBbvhuBNwC C7zw== X-Gm-Message-State: AOJu0YyG07GNKa2+gU77Wx285abgTUR2utBvdg2m4R6qdx8DqOycGUFJ VJttyCgy0FelpBaFzqkDD+8= X-Received: by 2002:a05:600c:cc:b0:405:3955:5872 with SMTP id u12-20020a05600c00cc00b0040539555872mr11841823wmm.18.1696796599599; Sun, 08 Oct 2023 13:23:19 -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.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Oct 2023 13:23:18 -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 0/4] Abstract vma_merge() and split_vma() Date: Sun, 8 Oct 2023 21:23:12 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Sun, 08 Oct 2023 13:23:36 -0700 (PDT) X-Spam-Level: ** The vma_merge() interface is very confusing and its implementation has led to numerous bugs as a result of that confusion. In addition there is duplication both in invocation of vma_merge(), but also in the common mprotect()-style pattern of attempting a merge, then if this fails, splitting the portion of a VMA about to have its attributes changed. This pattern has been copy/pasted around the kernel in each instance where such an operation has been required, each very slightly modified from the last to make it even harder to decipher what is going on. Simplify the whole thing by dividing the actual uses of vma_merge() and split_vma() into specific and abstracted functions and de-duplicate the vma_merge()/split_vma() pattern altogether. Doing so also opens the door to changing how vma_merge() is implemented - by knowing precisely what cases a caller is invoking rather than having a central interface where anything might happen, we can untangle the brittle and confusing vma_merge() implementation into something more workable. For mprotect()-like cases we introduce vma_modify() which performs the vma_merge()/split_vma() pattern, returning a pointer or an ERR_PTR(err) if the splits fail. This is an internal interface, as it is confusing having a number of different parameters available for fields that can be changed. Instead we split the kernel interface into four functions:- * vma_modify_flags() - Prepare to modify the VMA's flags. * vma_modify_flags_name() - Prepare to modify the VMA's flags/anon_vma_name * vma_modify_policy() - Prepare to modify the VMA's mempolicy. * vma_modify_uffd() - Prepare to modify the VMA's flags/uffd context. For cases where a new VMA is attempted to be merged with adjacent VMAs we add:- * vma_merge_new_vma() - Prepare to merge a new VMA. * vma_merge_extend() - Prepare to extend the end of a new VMA. Lorenzo Stoakes (4): mm: abstract the vma_merge()/split_vma() pattern for mprotect() et al. mm: make vma_merge() and split_vma() internal mm: abstract merge for new VMAs into vma_merge_new_vma() mm: abstract VMA extension and merge into vma_merge_extend() helper fs/userfaultfd.c | 53 +++++---------- include/linux/mm.h | 32 ++++++--- mm/internal.h | 7 ++ mm/madvise.c | 25 ++----- mm/mempolicy.c | 20 +----- mm/mlock.c | 24 ++----- mm/mmap.c | 160 ++++++++++++++++++++++++++++++++++++++++----- mm/mprotect.c | 27 ++------ mm/mremap.c | 30 ++++----- mm/nommu.c | 4 +- 10 files changed, 228 insertions(+), 154 deletions(-) -- 2.42.0