From patchwork Sun Oct 8 20:23:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13412795 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 2023EE95A80 for ; Sun, 8 Oct 2023 20:23:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 979896B015C; Sun, 8 Oct 2023 16:23:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 92A696B015E; Sun, 8 Oct 2023 16:23:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A4136B0165; Sun, 8 Oct 2023 16:23:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 699816B015C for ; Sun, 8 Oct 2023 16:23:25 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 474771CA4FB for ; Sun, 8 Oct 2023 20:23:25 +0000 (UTC) X-FDA: 81323419170.15.CDFE963 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) by imf25.hostedemail.com (Postfix) with ESMTP id 5DEE4A0013 for ; Sun, 8 Oct 2023 20:23:23 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="kOnZ5/Km"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.46 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=1696796603; 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=oh6ec6CTQesC51vfa/4MCcXzfoVwgVcGNsFkXLPVEho=; b=laYJ1pfFzs86lrxPhwwY8Pfr0G1C4YieGXyl99iPJ2NcASFTiC4+2f24TI5AzcAtTsHh5o Q61zsQZzQ2xgKoQyvXWU35dQMue/3iaugTeyO3a2fk4styHjLKLlXSOP7xyNM+YNGHbRk/ rIUshLDvYTmqgxNQh8nwOXZo6TWBwlo= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="kOnZ5/Km"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.46 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696796603; a=rsa-sha256; cv=none; b=ItSgr95GtjOeVToG4VJ4zsf32dKFqsFTCKHgWuPgDZRSwUVxFHuy+r2gRr1709TIJkN8nm 9T1mgqyMAh5tM7znWuJDNOFiNvguDPxQtSpiJscvladAj2NrgohfoW5s/05WgTWLP4yDdS MRTVo937EQbNO3Hlkj8209KGqRFwD6A= Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-406650da82bso35409305e9.3 for ; Sun, 08 Oct 2023 13:23:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696796601; x=1697401401; 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=oh6ec6CTQesC51vfa/4MCcXzfoVwgVcGNsFkXLPVEho=; b=kOnZ5/KmR7XzrQT/eE092t8Rv7gHCl19a5FY8HYxxHEoVrTBZRGRM1QYy3HGj32IMZ gR2SKt7LtzZwBQ3DL+mXuXwU8TKg+VKfdatNR+YToGNisNitMV3zqEhQXiGspW7EFV1E Ff1Ld2qoeIhULFO2uF2SLv2pXcGftJnuikgsijNQ5684fTJHxRpsAQQT0WzLyhPCOEqZ 7zZZU1Kxc0FNwHsqCkfAlXqlrhn/QVRp+vnh9MR3RiWJpCpeRyjwh4+Hm+V4K1JsqOQA 8JKDm6PTm+O3Ydjq4mouDEOs2S7yNRAyzg3HVIhnXBKVuTxj6zLh5+rbq0LdQauGY5HB 556g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696796601; x=1697401401; 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=oh6ec6CTQesC51vfa/4MCcXzfoVwgVcGNsFkXLPVEho=; b=QoLABBrkSiC1I/U2w2ePNKGOlNTtMsIrpe/CGlz0r5CaWBuP7rV/DZfp3ncXXt0buk gP2pDYkLTOnq0GdNBpannIrtdGVHQTmUmmnf4WldLZchV9uaDJCfQSV9t4d3qGcR5C1N zEbnqW4hrFDfWWZ1TTUhlskAVEB+Xs9MTfkRyentG/SgRH7Ux4f/LGoNimItumwxnoJe rH1RnxxR3qO0SxKrgJiY1GZsv5Jp1e4NLbppO9nYAsut8lxmDNLttL6DHQ4QW6UebXpJ dX+JtICFSpRl6Pi508Jt3ugMBId7UlmLSm+zDZ0lB3im0W3sd5X/yhIhyHmPSdebC3z8 rGxQ== X-Gm-Message-State: AOJu0Yy+pdIF71I5hM/oOdLEE5dVnIerarMarMYxOvyFv9y0tLuxfw7E 3PxXkqF4iF9NrSSfUMiJO4oK9LaNSH4= X-Google-Smtp-Source: AGHT+IFX6EUh5dGcLh467RUTdLkD7TfmQOxpSCn/MZRadvHnRtJtkVpbin/HqLminQaG/NJnzpjLCg== X-Received: by 2002:a1c:7419:0:b0:3fe:89be:cd3 with SMTP id p25-20020a1c7419000000b003fe89be0cd3mr11961603wmc.22.1696796601251; Sun, 08 Oct 2023 13:23:21 -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.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Oct 2023 13:23:20 -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 1/4] mm: abstract the vma_merge()/split_vma() pattern for mprotect() et al. Date: Sun, 8 Oct 2023 21:23:13 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 5DEE4A0013 X-Stat-Signature: 135gkq8r4hscoz65x9p6dkp8zi4iuh7a X-HE-Tag: 1696796603-905076 X-HE-Meta: U2FsdGVkX1/wuy/G2zik36bIb0aWfxq7sGNlPrfXahYb1Nfw6QGhJhhF+OGomgVRBPuDUT4XUIF/72TyrexEM3+3RBblkn1jBICz50gM2/7I/g7utyr5UBu475fbqrjpBHuJsItsiD26eQEMQJ6fZ1es9xfVp38BtejBaq09IxsDhZJMp98FDHtt4AHXFpRFq1OhoDDxQLipmiKHiIsUk7zwy2W1Mxwd/8EQ5OjIY2nf87rpv6a0BH/Vs1jLKpSDkxnqnxJsxWpuaUjxlectcv6IlMGCtlW4u6QPYDVOol9Om7iDQPvqmTBXbBeMf4mHSgq2k0jsStBXlnuuPNROQCPe9W285nxDVLCWJt4oaE7m/lxt8UQFgy+i6g1YkTZdhpsorw9GcpCy6SjOtOT+jDKgyyaJKERMfYlVbKxPT7Mnpce1p0+zK8TGKLo0Iojm1B/xwZb7WjFeeyQZa9FCk3iqJ3+/9FOQjFb6zORvd/F9Zp5c11ImXcu1A/px+KICLF3xy1n3FsNG0Sg5U4XXdRKN/1swL/RMBQ+ynFE6N3SC6LPSZ50HcDGULI1WWbPBpDusKyYbFaOzxV8Cw0RSqx62MRjHSrKkHvTu5vMZfSch9yCY67Z0nSNeqYYlby8YnjxnKPLNt9j7qzooIReAkCw+dgZfAfTJXJVPN3I/f/sHD0L9GUZCwAZVBVhrtmF5NFQVOcQY3Qm5HobHDgFg+FECTYb3HkmTmy9SD4r6PwHD9945tI2L/ZdDof4H6+Ty9pOD0dnTcB69B5oIwzkJPA8Gd6FJZwEZ9dqmGUQp4HF3kQhmXvneTB3F+KM5wIJpLelnFbdYe4e5Xuyubyiv1EnsUQ0bymTuJV0gQFLx6mjxAIXm8W9x019hmVkhQvv8qC0Lb+/JKAP+64tM/vA24kFvvOJTvRpbQ7eLLbaEZKxLQxVDPEB0/zdCa5HGX+S10TZJt9dsPaYtZYAiJLY KVpTX2EJ gA2udOBXECb7nTWq6XwnMdsJV2wCTWs+LXJ1kjbcVBXZjS5iAIiWseGIQuRM42+GuFUuCvFWzfJ+77kZzWV2ZA7+Ny/NqwVpafmz6laLiz/EPpN/rwgJSk9HpSD+TqCH3p+x033BGhtTHk8Nq5VhiEr6zp4dGFUYBFma8g4csUal9nop5J15kG9gmwYVgKsDm8u6VHyMlIG2Z6NTN/R6yU2YBe0TxQBwwXsJLG7gZ+g+dcVe12vjomNp7DT1YWUb+nkT4uBYajxwylo40YG1IAqQSw1nVRDbTZQss1g8+gaBZXfH/CyZ0trnsNlO58Aw2vUXF2lJBYCX/u44fkYuv86WvQdxpYBTOoG5KVbVcKSrUFuvViPsFEg4YnBWTuyA+fd2S7APtZmD0B9Zou9KNU9YYX2GgGsoq/BWpo4JrpPvhmgXZcwtx/0w44kQHQugiyKLj7llR0XMWAp+eXtxD5g4rkwwgyA8bC5Nr 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: 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 static 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 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 | 53 +++++++++----------------- include/linux/mm.h | 23 ++++++++++++ mm/madvise.c | 25 ++++--------- mm/mempolicy.c | 20 ++-------- mm/mlock.c | 24 ++++-------- mm/mmap.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++ mm/mprotect.c | 27 ++++---------- 7 files changed, 157 insertions(+), 108 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index a7c6ef764e63..9e5232d23927 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_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,26 +1482,18 @@ 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)); + prev = vma_modify_uffd(&vmi, prev, vma, start, vma_end, + new_flags, + ((struct vm_userfaultfd_ctx){ ctx })); 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; + + if (IS_ERR(prev)) { + ret = PTR_ERR(prev); + break; } next: /* @@ -1568,7 +1558,6 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, const void __user *buf = (void __user *)arg; struct vma_iterator vmi; bool wp_async = userfaultfd_wp_async_ctx(ctx); - pgoff_t pgoff; ret = -EFAULT; if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister))) @@ -1671,24 +1660,16 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, uffd_wp_range(vma, start, vma_end - start, false); new_flags = vma->vm_flags & ~__VM_UFFD_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), - NULL_VM_UFFD_CTX, anon_vma_name(vma)); + prev = vma_modify_uffd(&vmi, prev, vma, start, vma_end, + new_flags, NULL_VM_UFFD_CTX); if (prev) { 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; + + if (IS_ERR(prev)) { + ret = PTR_ERR(prev); + break; } next: /* diff --git a/include/linux/mm.h b/include/linux/mm.h index a7b667786cde..c069813f215f 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3253,6 +3253,29 @@ extern struct vm_area_struct *copy_vma(struct vm_area_struct **, unsigned long addr, unsigned long len, pgoff_t pgoff, bool *need_rmap_locks); extern void exit_mmap(struct mm_struct *); +struct vm_area_struct *vma_modify_flags(struct vma_iterator *vmi, + struct vm_area_struct *prev, + struct vm_area_struct *vma, + unsigned long start, unsigned long end, + unsigned long new_flags); +struct vm_area_struct *vma_modify_flags_name(struct vma_iterator *vmi, + struct vm_area_struct *prev, + struct vm_area_struct *vma, + unsigned long start, + unsigned long end, + unsigned long new_flags, + struct anon_vma_name *new_name); +struct vm_area_struct *vma_modify_policy(struct vma_iterator *vmi, + struct vm_area_struct *prev, + struct vm_area_struct *vma, + unsigned long start, unsigned long end, + struct mempolicy *new_pol); +struct vm_area_struct *vma_modify_uffd(struct vma_iterator *vmi, + struct vm_area_struct *prev, + struct vm_area_struct *vma, + unsigned long start, unsigned long end, + unsigned long new_flags, + struct vm_userfaultfd_ctx new_ctx); static inline int check_data_rlimit(unsigned long rlim, unsigned long new, diff --git a/mm/madvise.c b/mm/madvise.c index a4a20de50494..73024693d5c8 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -141,7 +141,7 @@ static int madvise_update_vma(struct vm_area_struct *vma, { struct mm_struct *mm = vma->vm_mm; int error; - pgoff_t pgoff; + struct vm_area_struct *merged; VMA_ITERATOR(vmi, mm, start); if (new_flags == vma->vm_flags && anon_vma_name_eq(anon_vma_name(vma), anon_name)) { @@ -149,28 +149,17 @@ static int madvise_update_vma(struct vm_area_struct *vma, return 0; } - pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); - *prev = vma_merge(&vmi, mm, *prev, start, end, new_flags, - vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), - vma->vm_userfaultfd_ctx, anon_name); - if (*prev) { - vma = *prev; + merged = vma_modify_flags_name(&vmi, *prev, vma, start, end, new_flags, + anon_name); + if (merged) { + vma = *prev = merged; goto success; } *prev = vma; - if (start != vma->vm_start) { - error = split_vma(&vmi, vma, start, 1); - if (error) - return error; - } - - if (end != vma->vm_end) { - error = split_vma(&vmi, vma, end, 0); - if (error) - return error; - } + if (IS_ERR(merged)) + return PTR_ERR(merged); success: /* vm_flags is protected by the mmap_lock held in write mode. */ diff --git a/mm/mempolicy.c b/mm/mempolicy.c index b01922e88548..b608b1744197 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -786,8 +786,6 @@ static int mbind_range(struct vma_iterator *vmi, struct vm_area_struct *vma, { struct vm_area_struct *merged; unsigned long vmstart, vmend; - pgoff_t pgoff; - int err; vmend = min(end, vma->vm_end); if (start > vma->vm_start) { @@ -802,26 +800,14 @@ static int mbind_range(struct vma_iterator *vmi, struct vm_area_struct *vma, return 0; } - pgoff = vma->vm_pgoff + ((vmstart - vma->vm_start) >> PAGE_SHIFT); - merged = vma_merge(vmi, vma->vm_mm, *prev, vmstart, vmend, vma->vm_flags, - vma->anon_vma, vma->vm_file, pgoff, new_pol, - vma->vm_userfaultfd_ctx, anon_vma_name(vma)); + merged = vma_modify_policy(vmi, *prev, vma, vmstart, vmend, new_pol); if (merged) { *prev = merged; return vma_replace_policy(merged, new_pol); } - if (vma->vm_start != vmstart) { - err = split_vma(vmi, vma, vmstart, 1); - if (err) - return err; - } - - if (vma->vm_end != vmend) { - err = split_vma(vmi, vma, vmend, 0); - if (err) - return err; - } + if (IS_ERR(merged)) + return PTR_ERR(merged); *prev = vma; return vma_replace_policy(vma, new_pol); diff --git a/mm/mlock.c b/mm/mlock.c index 42b6865f8f82..50ebea3b7885 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -476,10 +476,10 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, unsigned long end, vm_flags_t newflags) { struct mm_struct *mm = vma->vm_mm; - pgoff_t pgoff; int nr_pages; int ret = 0; vm_flags_t oldflags = vma->vm_flags; + struct vm_area_struct *merged; if (newflags == oldflags || (oldflags & VM_SPECIAL) || is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm) || @@ -487,25 +487,15 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, /* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */ goto out; - pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); - *prev = vma_merge(vmi, mm, *prev, start, end, newflags, - vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), - vma->vm_userfaultfd_ctx, anon_vma_name(vma)); - if (*prev) { - vma = *prev; + merged = vma_modify_flags(vmi, *prev, vma, start, end, newflags); + if (merged) { + vma = *prev = merged; goto success; } - if (start != vma->vm_start) { - ret = split_vma(vmi, vma, start, 1); - if (ret) - goto out; - } - - if (end != vma->vm_end) { - ret = split_vma(vmi, vma, end, 0); - if (ret) - goto out; + if (IS_ERR(merged)) { + ret = PTR_ERR(merged); + goto out; } success: diff --git a/mm/mmap.c b/mm/mmap.c index 673429ee8a9e..8c21171b431f 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2437,6 +2437,99 @@ int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, return __split_vma(vmi, vma, addr, new_below); } +/* + * We are about to modify one or multiple of a VMA's flags, policy, userfaultfd + * context and anonymous VMA name within the range [start, end). + * + * As a result, we might be able to merge the newly modified VMA range with an + * adjacent VMA with identical properties. + * + * If no merge is possible and the range does not span the entirety of the VMA, + * we then need to split the VMA to accommodate the change. + */ +static struct vm_area_struct *vma_modify(struct vma_iterator *vmi, + struct vm_area_struct *prev, + struct vm_area_struct *vma, + unsigned long start, unsigned long end, + unsigned long vm_flags, + struct mempolicy *policy, + struct vm_userfaultfd_ctx uffd_ctx, + struct anon_vma_name *anon_name) +{ + pgoff_t pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); + struct vm_area_struct *merged; + + merged = vma_merge(vmi, vma->vm_mm, prev, start, end, vm_flags, + vma->anon_vma, vma->vm_file, pgoff, policy, + uffd_ctx, anon_name); + if (merged) + return merged; + + if (vma->vm_start < start) { + int err = split_vma(vmi, vma, start, 1); + + if (err) + return ERR_PTR(err); + } + + if (vma->vm_end > end) { + int err = split_vma(vmi, vma, end, 0); + + if (err) + return ERR_PTR(err); + } + + return NULL; +} + +/* We are about to modify the VMA's flags. */ +struct vm_area_struct *vma_modify_flags(struct vma_iterator *vmi, + struct vm_area_struct *prev, + struct vm_area_struct *vma, + unsigned long start, unsigned long end, + unsigned long new_flags) +{ + return vma_modify(vmi, prev, vma, start, end, new_flags, + vma_policy(vma), vma->vm_userfaultfd_ctx, + anon_vma_name(vma)); +} + +/* We are about to modify the VMA's flags and/or anon_name. */ +struct vm_area_struct *vma_modify_flags_name(struct vma_iterator *vmi, + struct vm_area_struct *prev, + struct vm_area_struct *vma, + unsigned long start, + unsigned long end, + unsigned long new_flags, + struct anon_vma_name *new_name) +{ + return vma_modify(vmi, prev, vma, start, end, new_flags, + vma_policy(vma), vma->vm_userfaultfd_ctx, new_name); +} + +/* We are about to modify the VMA's flags memory policy. */ +struct vm_area_struct *vma_modify_policy(struct vma_iterator *vmi, + struct vm_area_struct *prev, + struct vm_area_struct *vma, + unsigned long start, unsigned long end, + struct mempolicy *new_pol) +{ + return vma_modify(vmi, prev, vma, start, end, vma->vm_flags, + new_pol, vma->vm_userfaultfd_ctx, anon_vma_name(vma)); +} + +/* We are about to modify the VMA's uffd context and/or flags. */ +struct vm_area_struct *vma_modify_uffd(struct vma_iterator *vmi, + struct vm_area_struct *prev, + struct vm_area_struct *vma, + unsigned long start, unsigned long end, + unsigned long new_flags, + struct vm_userfaultfd_ctx new_ctx) +{ + return vma_modify(vmi, prev, vma, start, end, new_flags, + vma_policy(vma), new_ctx, anon_vma_name(vma)); +} + /* * do_vmi_align_munmap() - munmap the aligned region from @start to @end. * @vmi: The vma iterator diff --git a/mm/mprotect.c b/mm/mprotect.c index b94fbb45d5c7..fdc94453bced 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -581,7 +581,7 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb, long nrpages = (end - start) >> PAGE_SHIFT; unsigned int mm_cp_flags = 0; unsigned long charged = 0; - pgoff_t pgoff; + struct vm_area_struct *merged; int error; if (newflags == oldflags) { @@ -625,31 +625,18 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb, } } - /* - * First try to merge with previous and/or next vma. - */ - pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); - *pprev = vma_merge(vmi, mm, *pprev, start, end, newflags, - vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), - vma->vm_userfaultfd_ctx, anon_vma_name(vma)); - if (*pprev) { - vma = *pprev; + merged = vma_modify_flags(vmi, *pprev, vma, start, end, newflags); + if (merged) { + vma = *pprev = merged; VM_WARN_ON((vma->vm_flags ^ newflags) & ~VM_SOFTDIRTY); goto success; } *pprev = vma; - if (start != vma->vm_start) { - error = split_vma(vmi, vma, start, 1); - if (error) - goto fail; - } - - if (end != vma->vm_end) { - error = split_vma(vmi, vma, end, 0); - if (error) - goto fail; + if (IS_ERR(merged)) { + error = PTR_ERR(merged); + goto fail; } success: