[PATCH 03/32] mm/shmem: reject userfaultfd fills during hole punch

From: David Hildenbrand (Arm)

Date: Sun Nov 29 2026 - 05:07:59 EST


While punching a shmem/tmpfs hole, regular page faults in the range wait
for the hole punch to finish, but UFFDIO_COPY, UFFDIO_ZEROPAGE and
UFFDIO_CONTINUE keep mapping pages into it. The hole punch must then
repeatedly walk the file's VMAs to unmap these pages, while holding
i_rwsem.

Consequently, punching a hole can become very slow if userfaultfd keeps
filling the range, blocking other writers. Any user that can read the
file can trigger that through a MAP_PRIVATE mapping and UFFDIO_CONTINUE.

Let's fix it by making UFFDIO_COPY, UFFDIO_ZEROPAGE and UFFDIO_CONTINUE
return -EAGAIN when faults in the range are blocked, so user space can
retry, instead of waiting in the uffd code.

Returning -EAGAIN simplifies the implementation. These ioctls already
return -EAGAIN, including before making any progress: for example,
UFFDIO_CONTINUE can encounter this when khugepaged removes a shmem page
table concurrently.

QEMU, for example, prevents concurrent RAM discards and page placement
during incoming postcopy entirely. Applications that allow concurrent
hole punching and do not handle -EAGAIN could be affected, although
existing races would already require proper -EAGAIN handling.

The existing uffd code already handles partial progress and wakes
resolved faults properly.

Share the range check with shmem_falloc_wait() and preserve -EAGAIN from
the filesystem in the UFFDIO_CONTINUE path.

This has medium urgency, as unprivileged users can make a hole punch,
and writers of the file, stall for minutes.

Fixes: 4c27fe4c4c84 ("userfaultfd: shmem: add shmem_mcopy_atomic_pte for userfaultfd support")
Cc: stable@xxxxxxxxxxxxxxx
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
mm/shmem.c | 34 ++++++++++++++++++++++++++++++----
mm/userfaultfd.c | 3 +++
2 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/mm/shmem.c b/mm/shmem.c
index afe6c80b6f48c..ce8f8b5f00ed7 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2971,6 +2971,16 @@ static int synchronous_wake_function(wait_queue_entry_t *wait,
return ret;
}

+/* Disallow placing folios while hole-punch or a pin scan is in progress. */
+static bool __shmem_faults_blocked(struct inode *inode, pgoff_t pgoff)
+{
+ struct shmem_falloc *falloc = inode->i_private;
+
+ lockdep_assert_held(&inode->i_lock);
+ return falloc && falloc->waitq &&
+ pgoff >= falloc->start && pgoff < falloc->next;
+}
+
/*
* Trinity finds that probing a hole which tmpfs is punching can
* prevent the hole-punch from ever completing: which in turn
@@ -2996,10 +3006,7 @@ static vm_fault_t shmem_falloc_wait(struct vm_fault *vmf, struct inode *inode)

spin_lock(&inode->i_lock);
shmem_falloc = inode->i_private;
- if (shmem_falloc &&
- shmem_falloc->waitq &&
- vmf->pgoff >= shmem_falloc->start &&
- vmf->pgoff < shmem_falloc->next) {
+ if (__shmem_faults_blocked(inode, vmf->pgoff)) {
wait_queue_head_t *shmem_falloc_waitq;
DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function);

@@ -3490,6 +3497,19 @@ static struct folio *shmem_mfill_folio_alloc(struct vm_area_struct *vma,
return folio;
}

+static bool shmem_faults_blocked(struct inode *inode, pgoff_t pgoff)
+{
+ bool blocked;
+
+ if (likely(!READ_ONCE(inode->i_private)))
+ return false;
+
+ spin_lock(&inode->i_lock);
+ blocked = __shmem_faults_blocked(inode, pgoff);
+ spin_unlock(&inode->i_lock);
+ return blocked;
+}
+
static int shmem_mfill_filemap_add(struct folio *folio,
struct vm_area_struct *vma,
unsigned long addr)
@@ -3500,6 +3520,9 @@ static int shmem_mfill_filemap_add(struct folio *folio,
gfp_t gfp = mapping_gfp_mask(mapping);
int err;

+ if (shmem_faults_blocked(inode, pgoff))
+ return -EAGAIN;
+
__folio_set_locked(folio);
__folio_set_swapbacked(folio);

@@ -3539,6 +3562,9 @@ static struct folio *shmem_get_folio_noalloc(struct inode *inode, pgoff_t pgoff)
struct folio *folio;
int err;

+ if (shmem_faults_blocked(inode, pgoff))
+ return ERR_PTR(-EAGAIN);
+
err = shmem_get_folio(inode, pgoff, 0, &folio, SGP_NOALLOC);
if (err)
return ERR_PTR(err);
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 15ad69ec2fc01..8ee1c7eb10d59 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -683,6 +683,9 @@ static int mfill_atomic_pte_continue(struct mfill_state *state)
}

folio = ops->get_folio_noalloc(inode, pgoff);
+ /* Preserve retry requests from the filesystem. */
+ if (IS_ERR(folio) && PTR_ERR(folio) == -EAGAIN)
+ return -EAGAIN;
/* Our caller expects us to return -EFAULT if we failed to find folio */
if (IS_ERR_OR_NULL(folio))
return -EFAULT;

--
2.56.1