mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: Andrew Morton <akpm@linux-foundation.org>,
	 "Liam R. Howlett" <liam@infradead.org>,
	 David Hildenbrand <david@kernel.org>,
	Vlastimil Babka <vbabka@kernel.org>,
	 Jann Horn <jannh@google.com>, Mike Rapoport <rppt@kernel.org>,
	 Suren Baghdasaryan <surenb@google.com>,
	Michal Hocko <mhocko@suse.com>,  Shuah Khan <shuah@kernel.org>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	 linux-kselftest@vger.kernel.org, ljs@kernel.org,
	 Tal Zussman <tz2294@columbia.edu>,
	stable@vger.kernel.org
Subject: [PATCH] mm/madvise: disallow guard region installation in uffd-armed VMAs
Date: Thu, 12 Nov 2026 17:38:26 +0000	[thread overview]
Message-ID: <20261112-disallow-uffd-guard-install-v1-1-bcf8b1aaa72e@kernel.org> (raw)

In order to establish guard regions, guard markers are installed in the
page tables of a mapped VMA.

However, problems arise when one tries to combine this with registering a
VMA for userfaultfd, as such operations may themselves need to install page
table markers such as uffd-wp.

When this installation is attempted the guard marker prevents these markers
being applied, which can lead to unexpected results (especially if the
guard regions are later removed.)

This is especially problematic because guard region installation causes the
regions to be zapped first, meaning that the removal of userfaultfd-tracked
ranges will not be logged in this case.

Therefore, disallow this.

This makes sense as conceptually, guard regions mark a part of a VMA as
effectively unmapped at the page table level, and removing the markers is
like mapping the VMA again, over which operation uffd markers would in any
case be lost.

Only check for this on installation, as a VMA can be registered for
userfaultfd after guard regions are established.

In this case, the guard regions cannot have new markers installed, and no
faults can occur in the guard regions, until they are removed, at which
point everything should function normally.

A survey of open source users indicates nobody is relying on installing
guard regions in uffd-armed VMAs. Software including glibc, android,
tcmalloc and CRIU were checked.

Fix the selftest to reflect the change.

Fixes: 662df3e5c376 ("mm: madvise: implement lightweight guard page mechanism")
Reported-by: Tal Zussman <tz2294@columbia.edu>
Closes: https://lore.kernel.org/all/20261111-uffd-state-fixes-v1-9-00713d368976@columbia.edu/
Closes: https://lore.kernel.org/all/20261111-uffd-state-fixes-v1-10-00713d368976@columbia.edu/
Closes: https://lore.kernel.org/all/20261111-uffd-state-fixes-v1-11-00713d368976@columbia.edu/
Cc: stable@vger.kernel.org
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
 mm/madvise.c                               |  4 ++++
 tools/testing/selftests/mm/guard-regions.c | 12 ++++++++----
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/mm/madvise.c b/mm/madvise.c
index 0214efd2a014..268731e90f12 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -1156,6 +1156,10 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)
 	if (!is_valid_guard_vma(vma, /* allow_locked = */false))
 		return -EINVAL;
 
+	/* Guard markers are incompatible with UFFD markers. */
+	if (userfaultfd_armed(vma))
+		return -EINVAL;
+
 	/*
 	 * Set atomically under read lock. All pertinent readers will need to
 	 * acquire an mmap/VMA write lock to read it. All remaining readers may
diff --git a/tools/testing/selftests/mm/guard-regions.c b/tools/testing/selftests/mm/guard-regions.c
index 02f69244f9a4..97da54dcf3a4 100644
--- a/tools/testing/selftests/mm/guard-regions.c
+++ b/tools/testing/selftests/mm/guard-regions.c
@@ -1476,7 +1476,7 @@ TEST_F(guard_regions, cold_pageout)
 	ASSERT_EQ(munmap(ptr, 10 * page_size), 0);
 }
 
-/* Ensure that guard pages do not break userfaultd. */
+/* Ensure that guard regions cannot be installed in uffd-armed VMAs. */
 TEST_F(guard_regions, uffd)
 {
 	const unsigned long page_size = self->page_size;
@@ -1526,10 +1526,15 @@ TEST_F(guard_regions, uffd)
 	reg.mode = UFFDIO_REGISTER_MODE_MISSING;
 	ASSERT_EQ(ioctl(uffd, UFFDIO_REGISTER, &reg), 0);
 
-	/* Guard the range. This should not trigger the uffd. */
+	/* Now try to guard, should fail with EINVAL. */
+	ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_INSTALL), -1);
+	ASSERT_EQ(errno, EINVAL);
+
+	/* Once unregistered, we should be able to guard the range. */
+	ASSERT_EQ(ioctl(uffd, UFFDIO_UNREGISTER, &range), 0);
 	ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_INSTALL), 0);
 
-	/* The guarding should behave as usual with no uffd intervention. */
+	/* The guarding should behave as usual. */
 	for (i = 0; i < 10; i++) {
 		char *curr = &ptr[i * page_size];
 
@@ -1537,7 +1542,6 @@ TEST_F(guard_regions, uffd)
 	}
 
 	/* Cleanup. */
-	ASSERT_EQ(ioctl(uffd, UFFDIO_UNREGISTER, &range), 0);
 	close(uffd);
 	ASSERT_EQ(munmap(ptr, 10 * page_size), 0);
 }

---
base-commit: 4318887a3efa4c53d6f083ccfd7105b809a3ac65
change-id: 20261112-disallow-uffd-guard-install-a5294129ad7b

Cheers,
-- 
Lorenzo Stoakes (ARM) <ljs@kernel.org>


                 reply	other threads:[~2026-11-12 17:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261112-disallow-uffd-guard-install-v1-1-bcf8b1aaa72e@kernel.org \
    --to=ljs@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=jannh@google.com \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@suse.com \
    --cc=rppt@kernel.org \
    --cc=shuah@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=surenb@google.com \
    --cc=tz2294@columbia.edu \
    --cc=vbabka@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®