From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796388683; b=LuFxKY+n6ATlbyGCWW4zwhVyV5xKZF2DmcobC5LQOCzTUZdUAB8U4j8ivvv+QrQz3zQh fwwhyAX8/pqeJ+/AbSQP2WCcFwCCOJH+B9D9DqyBhIH1VIp4HyxU6d/JUCeQ3n3xi6CL/ 2OIZxCIPiEaRgG7jnWJaORqsxwUDdCVWibBI4LurA31ga0jJJQ3PWEvY5fn515gofTR8H YrDF1yiLyjUi/tAiDe0OTggGcXvTy2d748i9eFiRXylDYW97kEXYLiA3c/qXiHIMiGXgQ I+/qUnWuvQicGAhYOJwwkBs6HAFNjsTz+G8WuUoxcHKVbXa0EJSQYHVJ3DC5V83DVnQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388683; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=Vw6tgoqLl9dcfvhegXK+X16GmPg6kDM2KzsAMnu4enE=; b=18NcQc2qgKJYYSP1Fa4RhI2lfldx0tSoEHRidGbsgfXl5Xx0pfQfTgIIw4xzFCELDalS BlbiFD61/eT3cnHqK2AbtHld3KlS3PaF6PeK5PtqkgbqIuu+gF6VjV9BAARF2I3AOgjN2 uOY2co73uWCpzqUnSr/CnyPaw3cYvHhPGxSQS8TAI5RsjWdtYeG3pkxaIIDTTLlnkgHR3 c5HIsN1FTfcbSNsxX+THF2DpTN+PqZbmdcEu/CP7yfQy5dK9e5T1tnRhwPWRAXdLJsUVi Wd96PDb7XE9W7vHpLjUtXaVpq/POHQaAwFmp5kUKLr6Bl+IRsIN/MN0delbizShw3Dg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=P87Vy4M1; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 035E2606A32C; Fri, 4 Dec 2026 12:51:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=P87Vy4M1 Received: by smtp.kernel.org (Postfix) with ESMTPSA id BF7911F0093A; Fri, 4 Dec 2026 12:51:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796388682; bh=Vw6tgoqLl9dcfvhegXK+X16GmPg6kDM2KzsAMnu4enE=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=P87Vy4M1OJzmqAyRIscFwhf0YzADoHgMMIbTkAiH8ros521FdTjb1ZwH5Pl1nTLm+ lab4aHYIdFtZFZlPFEy9m20N2lXOLcNJ4vc+iDCbQwuy+JfVAHurgKqJUmXG4vF/Yq NslFKdVuiLVWlwRluAxMnVRj+Aar62+92O+2y3Be9JaE0tDNvS0GemC6RG7dldJroD zs6W/U0WixUR7QdLGrMjXFQ9yLUmcwHCLdtDHurX7Acc2IQJBDrCxYfnvo/7dTk8be s8Owvl8hRfaWLcRMjSnrfzYmjmpvQffKyBvaS/P8j/PKuURYnM0MYjXXln8NtOO0GQ x7PCa6+kyNd9g== From: "David Hildenbrand (Arm)" Date: Fri, 04 Dec 2026 13:49:54 +0100 Subject: [PATCH v2 03/31] mm/shmem: reject userfaultfd fills during hole punch X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261204-fixes_core_3-v2-3-9aad65727632@kernel.org> References: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> In-Reply-To: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> To: Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, SJ Park , "David Hildenbrand (Arm)" , stable@vger.kernel.org, Chris Mason , "Jose A. Perez de Azpillaga" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4944; i=david@kernel.org; h=from:subject:message-id; bh=s9t+vzFBr4LD9VuRgUwmKMKgPx2wsQsQ6cqkyZcqjOQ=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKFtv2b4MSlUMlx+87BuqZYUf2bs053zdj4o1qDK9r2z 13f7l1qHaUsDGIcDLJiiixBwfcNXr5cV1vtxscMM4eVCWQIAxenAEwk7DQjw0OTyiN3fZSOMifW snO/cPxrMCdBNWt6STDXecvQwIoLYQz/SzUOJ8Tl+m5TZVq78KBMnEf0ogfvJkf6PZvB3fJztUU NMwA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A 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@vger.kernel.org Reported-by: Chris Mason Reviewed-by: Jose A. Perez de Azpillaga Tested-by: Gregory Price (Meta) Reviewed-by: Gregory Price (Meta) Reviewed-by: Baolin Wang Signed-off-by: David Hildenbrand (Arm) --- mm/shmem.c | 33 +++++++++++++++++++++++++++++---- mm/userfaultfd.c | 3 +++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index a302ec07b36fd..7dfef65cfdbc4 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2973,6 +2973,15 @@ static int synchronous_wake_function(wait_queue_entry_t *wait, return ret; } +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 @@ -2998,10 +3007,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); @@ -3492,6 +3498,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) @@ -3502,6 +3521,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); @@ -3541,6 +3563,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 65f7399e79b65..d7a9ca70fb954 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