From nobody Sun Feb 8 11:44:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A0D036CDEC; Tue, 27 Jan 2026 19:29:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769542194; cv=none; b=bQrlHwzaLAgznD3apDxfTQpOEJfdfOEscFxVprHJsaVCbLfqm3KEZ0M79r5XGm8RsH8I84ulA4oCF6MsWRUjexqPM4G3cQPbKAVxL8RX4RLBpPd62NdNTRtEzw8PlE1RJ8DhBYCzGahxSZ4wgMqwc+CNmVh44W7Y4brOa6yRar4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769542194; c=relaxed/simple; bh=aI4WO62kph2VvKe2gJ7QoMhXLXNy49H9C+58Y9P8ebA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WvVRdeaox9scboCyy5t7aqpE9t6uNAgUwq7ZxyIX9O50FDbnwreznFwf64Tb6SbPNvW68wKjrja3pmDwFHStPMGOJU6IQpdPxUC+BI/w9CLtNmNlY7/oQMhKSlnAcdXoaD1w8pGHNuwIQ3z1K7CjEJaK22dnU/IJ1nebjQPHbzE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uzu2dY0F; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="uzu2dY0F" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D53D3C19425; Tue, 27 Jan 2026 19:29:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1769542193; bh=aI4WO62kph2VvKe2gJ7QoMhXLXNy49H9C+58Y9P8ebA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uzu2dY0FD8NP7vGfp5IudjeX8x6QiJOSfV1oxxuPXDUvu4rBKpUTNA49h5T9Wohpy pfEABMoxK6o/yC7roNa0yXjcHxBqwtIkm+UYpNDDQd1qRNy4ch60sgcDSqaVtG27wK K+UqBFc5dhddJewp0K/vzuLPF2PxpQi8HyATmV0Xi9XEzGTqVFNmA9w4LSuWcFXoVg QrYaAotmrdV0CDUshJOjvcVA2VrJFYEjysfOFyw5eFeR2wtvfX00eoewjFdNTtWcua +mBnHhBZ1o53N7kxZ4CnCivm92wleRp8YgLjcWxCGw1GfNjWI6wCViPGzu/VwKzk7f /HCrBMvTm8PjQ== From: Mike Rapoport To: linux-mm@kvack.org Cc: Andrea Arcangeli , Andrew Morton , Axel Rasmussen , Baolin Wang , David Hildenbrand , Hugh Dickins , James Houghton , "Liam R. Howlett" , Lorenzo Stoakes , Michal Hocko , Mike Rapoport , Muchun Song , Nikita Kalyazin , Oscar Salvador , Paolo Bonzini , Peter Xu , Sean Christopherson , Shuah Khan , Suren Baghdasaryan , Vlastimil Babka , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH RFC 01/17] userfaultfd: introduce mfill_copy_folio_locked() helper Date: Tue, 27 Jan 2026 21:29:20 +0200 Message-ID: <20260127192936.1250096-2-rppt@kernel.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260127192936.1250096-1-rppt@kernel.org> References: <20260127192936.1250096-1-rppt@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Mike Rapoport (Microsoft)" Split copying of data when locks held from mfill_atomic_pte_copy() into a helper function mfill_copy_folio_locked(). This makes improves code readability and makes complex mfill_atomic_pte_copy() function easier to comprehend. No functional change. Signed-off-by: Mike Rapoport (Microsoft) Acked-by: Peter Xu --- mm/userfaultfd.c | 59 ++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index e6dfd5f28acd..a0885d543f22 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -238,6 +238,40 @@ int mfill_atomic_install_pte(pmd_t *dst_pmd, return ret; } =20 +static int mfill_copy_folio_locked(struct folio *folio, unsigned long src_= addr) +{ + void *kaddr; + int ret; + + kaddr =3D kmap_local_folio(folio, 0); + /* + * The read mmap_lock is held here. Despite the + * mmap_lock being read recursive a deadlock is still + * possible if a writer has taken a lock. For example: + * + * process A thread 1 takes read lock on own mmap_lock + * process A thread 2 calls mmap, blocks taking write lock + * process B thread 1 takes page fault, read lock on own mmap lock + * process B thread 2 calls mmap, blocks taking write lock + * process A thread 1 blocks taking read lock on process B + * process B thread 1 blocks taking read lock on process A + * + * Disable page faults to prevent potential deadlock + * and retry the copy outside the mmap_lock. + */ + pagefault_disable(); + ret =3D copy_from_user(kaddr, (const void __user *) src_addr, + PAGE_SIZE); + pagefault_enable(); + kunmap_local(kaddr); + + if (ret) + return -EFAULT; + + flush_dcache_folio(folio); + return ret; +} + static int mfill_atomic_pte_copy(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, @@ -245,7 +279,6 @@ static int mfill_atomic_pte_copy(pmd_t *dst_pmd, uffd_flags_t flags, struct folio **foliop) { - void *kaddr; int ret; struct folio *folio; =20 @@ -256,27 +289,7 @@ static int mfill_atomic_pte_copy(pmd_t *dst_pmd, if (!folio) goto out; =20 - kaddr =3D kmap_local_folio(folio, 0); - /* - * The read mmap_lock is held here. Despite the - * mmap_lock being read recursive a deadlock is still - * possible if a writer has taken a lock. For example: - * - * process A thread 1 takes read lock on own mmap_lock - * process A thread 2 calls mmap, blocks taking write lock - * process B thread 1 takes page fault, read lock on own mmap lock - * process B thread 2 calls mmap, blocks taking write lock - * process A thread 1 blocks taking read lock on process B - * process B thread 1 blocks taking read lock on process A - * - * Disable page faults to prevent potential deadlock - * and retry the copy outside the mmap_lock. - */ - pagefault_disable(); - ret =3D copy_from_user(kaddr, (const void __user *) src_addr, - PAGE_SIZE); - pagefault_enable(); - kunmap_local(kaddr); + ret =3D mfill_copy_folio_locked(folio, src_addr); =20 /* fallback to copy_from_user outside mmap_lock */ if (unlikely(ret)) { @@ -285,8 +298,6 @@ static int mfill_atomic_pte_copy(pmd_t *dst_pmd, /* don't free the page */ goto out; } - - flush_dcache_folio(folio); } else { folio =3D *foliop; *foliop =3D NULL; --=20 2.51.0