[PATCH 09/32] mm/khugepaged: check for uffd outside the i_pages lock

From: David Hildenbrand (Arm)

Date: Sun Nov 29 2026 - 05:09:54 EST


collapse_file() walks the VMAs mapping a shmem range to check for UFFD
missing registrations before filling holes. It does this with the
i_pages lock held and interrupts disabled.

Consequently, a large number of mappings can cause excessive interrupt
latency.

Let's fix it by moving the walk before taking the i_pages lock. Hold
i_mmap_rwsem for read until the retry entries are stored, and take it for
write when registering UFFD missing mode on a file-backed VMA. This
prevents new missing-mode registrations between checking the VMAs and
installing the retry entries.

Move hole counting and retry-entry insertion into
collapse_file_mark_holes().

Also pass end - 1 to mapping_rmap_tree_foreach(), which takes an
inclusive last index. Otherwise, a UFFD missing-mode registration
immediately beyond the collapse range can incorrectly prevent collapse.

This has medium urgency, as unprivileged users can trigger it.

Fixes: ac492b9c70ca ("mm/khugepaged: skip shmem with userfaultfd")
Cc: stable@xxxxxxxxxxxxxxx
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
mm/khugepaged.c | 61 ++++++++++++++++++++++++++++++++------------------------
mm/userfaultfd.c | 12 +++++++++++
2 files changed, 47 insertions(+), 26 deletions(-)

diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 7296673378c72..5fa9e67351332 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -2232,6 +2232,25 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
i_mmap_unlock_read(mapping);
}

+static int collapse_file_mark_holes(struct xa_state *xas,
+ pgoff_t start, pgoff_t end)
+{
+ pgoff_t index;
+ int nr_none = 0;
+
+ xas_set(xas, start);
+ for (index = start; index < end; index++) {
+ if (xas_next(xas))
+ continue;
+ xas_store(xas, XA_RETRY_ENTRY);
+ if (xas_error(xas))
+ return xas_error(xas);
+ nr_none++;
+ }
+
+ return nr_none;
+}
+
/**
* collapse_file - collapse filemap/tmpfs/shmem pages into huge one.
*
@@ -2247,8 +2266,8 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
* + swap/gup in pages if necessary;
* - copy data to new page
* - handle shmem holes
- * + re-validate that holes weren't filled by someone else
* + check for userfaultfd
+ * + re-validate that holes weren't filled by someone else
* - finalize updates to the page cache;
* - if replacing succeeds:
* + unlock huge page;
@@ -2555,27 +2574,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,

if (nr_none) {
struct vm_area_struct *vma;
- int nr_none_check = 0;
+ int nr_none_check;

i_mmap_lock_read(mapping);
- xas_lock_irq(&xas);
-
- xas_set(&xas, start);
- for (index = start; index < end; index++) {
- if (!xas_next(&xas)) {
- xas_store(&xas, XA_RETRY_ENTRY);
- if (xas_error(&xas)) {
- result = SCAN_STORE_FAILED;
- goto immap_locked;
- }
- nr_none_check++;
- }
- }
-
- if (nr_none != nr_none_check) {
- result = SCAN_PAGE_FILLED;
- goto immap_locked;
- }

/*
* If userspace observed a missing page in a VMA with
@@ -2586,19 +2587,27 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
* guarantees that the kernel doesn't fill a missing
* page with a zero page, so they don't matter here.
*
- * Any userfaultfds registered after this point will
- * not be able to observe any missing pages due to the
- * previously inserted retry entries.
+ * i_mmap_rwsem syncs against concurrent userfaultfd MISSING
+ * registration in userfaultfd_set_ctx(). We replace holes with
+ * retry entries before dropping i_mmap_rwsem, so later
+ * registrations cannot observe missing pages.
*/
- mapping_rmap_tree_foreach(vma, mapping, start, end) {
+ mapping_rmap_tree_foreach(vma, mapping, start, end - 1) {
if (userfaultfd_missing(vma)) {
+ i_mmap_unlock_read(mapping);
result = SCAN_EXCEED_NONE_PTE;
- goto immap_locked;
+ goto rollback;
}
+ cond_resched();
}

-immap_locked:
+ xas_lock_irq(&xas);
+ nr_none_check = collapse_file_mark_holes(&xas, start, end);
i_mmap_unlock_read(mapping);
+ if (nr_none_check < 0)
+ result = SCAN_STORE_FAILED;
+ else if (nr_none != nr_none_check)
+ result = SCAN_PAGE_FILLED;
if (result != SCAN_SUCCEED) {
xas_set(&xas, start);
for (index = start; index < end; index++) {
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 8ee1c7eb10d59..09e383370b48f 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -2193,10 +2193,22 @@ static void userfaultfd_set_ctx(struct vm_area_struct *vma,
struct userfaultfd_ctx *ctx,
vm_flags_t vm_flags)
{
+ struct address_space *mapping = NULL;
+
vma_start_write(vma);
+
+ /* Sync with collapse_file() scanning for VM_UFFD_MISSING VMAs. */
+ if (vma->vm_file && (vm_flags & VM_UFFD_MISSING)) {
+ mapping = vma->vm_file->f_mapping;
+ i_mmap_lock_write(mapping);
+ }
+
vma->vm_userfaultfd_ctx = (struct vm_userfaultfd_ctx){ctx};
userfaultfd_set_vm_flags(vma,
(vma->vm_flags & ~__VM_UFFD_FLAGS) | vm_flags);
+
+ if (mapping)
+ i_mmap_unlock_write(mapping);
}

static void userfaultfd_reset_ctx(struct vm_area_struct *vma)

--
2.56.1