* [PATCH v2 1/9] userfaultfd: use anonymous memory for UFFDIO_ZEROPAGE on private file mappings
2026-11-16 20:02 [PATCH v2 0/9] userfaultfd: assorted fixes (part 1) Tal Zussman
@ 2026-11-16 20:02 ` Tal Zussman
2026-11-16 20:15 ` David Hildenbrand (Arm)
2026-11-16 20:16 ` David Hildenbrand (Arm)
2026-11-16 20:02 ` [PATCH v2 2/9] userfaultfd: refuse UFFDIO_MOVE into a non-present huge destination PMD Tal Zussman
` (7 subsequent siblings)
8 siblings, 2 replies; 20+ messages in thread
From: Tal Zussman @ 2026-11-16 20:02 UTC (permalink / raw)
To: Andrew Morton, Mike Rapoport, Peter Xu, James Houghton,
Andrea Arcangeli, Suren Baghdasaryan, Balbir Singh,
Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka, Jann Horn,
Pedro Falcato, David Hildenbrand, Michal Hocko,
Michał Mirosław, Andrei Vagin, Muhammad Usama Anjum,
Stephen Rothwell, Arnd Bergmann, Zi Yan, Matthew Brost,
Joshua Hahn, Byungchul Park, Gregory Price, Ying Huang,
Alistair Popple, Baolin Wang, Nico Pache, Ryan Roberts, Dev Jain,
Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau, Muchun Song,
Oscar Salvador, Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel, stable, Tal Zussman
If the mm cannot map the shared zero page, UFFDIO_ZEROPAGE falls back to
mfill_atomic_pte_zeroed_folio(), which allocates a zeroed folio and
installs it through vma_uffd_ops(), no matter what kind of mapping it
is. An s390 KVM guest with storage keys or protected virtualization is
such an mm.
For a MAP_PRIVATE shmem mapping, that adds the zeroed folio to the page
cache of the file. That is wrong: a private mapping must never modify
the file it maps, and the file might even be mapped from a read-only fd.
The ioctl can also fail where it should succeed. A missing fault is only
delivered for a hole in the file, but UFFDIO_ZEROPAGE can be called on
an address that did not fault, or can race with a write to the file. If
the file has a page at that offset by then, inserting the zeroed folio
fails and the ioctl returns -EEXIST although nothing is mapped at the
destination address.
What we want is what UFFDIO_ZEROPAGE does on a private mapping when the
shared zero page can be used, and what UFFDIO_COPY does: only check that
nothing is mapped at the destination address, map zeroed memory there,
and leave the page cache alone. The zeroed folio path did that before it
was moved to vm_uffd_ops.
To fix it, pick the ops in the common folio path, as
mfill_atomic_pte_copy() did for UFFDIO_COPY: anon_uffd_ops for a private
mapping, the ops of the VMA otherwise. That makes
mfill_atomic_pte_copy() and mfill_atomic_pte_zeroed_folio() redundant,
so remove them and rename __mfill_atomic_pte() to
mfill_atomic_pte_folio().
UFFDIO_ZEROPAGE no longer modifies the file, and only fails with -EEXIST
if something is already mapped at the destination address.
Fixes: f74991b4e383 ("shmem, userfaultfd: implement shmem uffd operations using vm_uffd_ops")
Cc: stable@vger.kernel.org
Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
mm/userfaultfd.c | 46 ++++++++++++++++------------------------------
1 file changed, 16 insertions(+), 30 deletions(-)
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index d65184d9a480..61a712e1f9f5 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -531,15 +531,27 @@ static int mfill_copy_folio_retry(struct mfill_state *mfill_state,
return 0;
}
-static int __mfill_atomic_pte(struct mfill_state *state,
- const struct vm_uffd_ops *ops)
+static int mfill_atomic_pte_folio(struct mfill_state *state)
{
+ const struct vm_uffd_ops *ops = vma_uffd_ops(state->vma);
unsigned long dst_addr = state->dst_addr;
unsigned long src_addr = state->src_addr;
uffd_flags_t flags = state->flags;
struct folio *folio;
int ret;
+ /*
+ * The normal page fault path for a MAP_PRIVATE mapping in a
+ * file-backed VMA will invoke the fault, fill the hole in the file and
+ * COW it right away. The result generates plain anonymous memory.
+ * So when we are asked to fill a hole in a MAP_PRIVATE mapping, we'll
+ * generate anonymous memory directly without actually filling the
+ * hole. For the MAP_PRIVATE case the robustness check only happens in
+ * the pagetable (to verify it's still none) and not in the page cache.
+ */
+ if (!(state->vma->vm_flags & VM_SHARED))
+ ops = &anon_uffd_ops;
+
if (!ops) {
VM_WARN_ONCE(1, "UFFDIO_COPY for unsupported VMA");
return -EOPNOTSUPP;
@@ -597,32 +609,6 @@ static int __mfill_atomic_pte(struct mfill_state *state,
return ret;
}
-static int mfill_atomic_pte_copy(struct mfill_state *state)
-{
- const struct vm_uffd_ops *ops = vma_uffd_ops(state->vma);
-
- /*
- * The normal page fault path for a MAP_PRIVATE mapping in a
- * file-backed VMA will invoke the fault, fill the hole in the file and
- * COW it right away. The result generates plain anonymous memory.
- * So when we are asked to fill a hole in a MAP_PRIVATE mapping, we'll
- * generate anonymous memory directly without actually filling the
- * hole. For the MAP_PRIVATE case the robustness check only happens in
- * the pagetable (to verify it's still none) and not in the page cache.
- */
- if (!(state->vma->vm_flags & VM_SHARED))
- ops = &anon_uffd_ops;
-
- return __mfill_atomic_pte(state, ops);
-}
-
-static int mfill_atomic_pte_zeroed_folio(struct mfill_state *state)
-{
- const struct vm_uffd_ops *ops = vma_uffd_ops(state->vma);
-
- return __mfill_atomic_pte(state, ops);
-}
-
static int mfill_atomic_pte_zeropage(struct mfill_state *state)
{
struct vm_area_struct *dst_vma = state->vma;
@@ -634,7 +620,7 @@ static int mfill_atomic_pte_zeropage(struct mfill_state *state)
if (mm_forbids_zeropage(dst_vma->vm_mm) ||
(dst_vma->vm_flags & VM_SHARED))
- return mfill_atomic_pte_zeroed_folio(state);
+ return mfill_atomic_pte_folio(state);
_dst_pte = pte_mkspecial(pfn_pte(zero_pfn(dst_addr),
dst_vma->vm_page_prot));
@@ -927,7 +913,7 @@ static __always_inline ssize_t mfill_atomic_pte(struct mfill_state *state)
if (uffd_flags_mode_is(flags, MFILL_ATOMIC_POISON))
return mfill_atomic_pte_poison(state);
if (uffd_flags_mode_is(flags, MFILL_ATOMIC_COPY))
- return mfill_atomic_pte_copy(state);
+ return mfill_atomic_pte_folio(state);
if (uffd_flags_mode_is(flags, MFILL_ATOMIC_ZEROPAGE))
return mfill_atomic_pte_zeropage(state);
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [PATCH v2 1/9] userfaultfd: use anonymous memory for UFFDIO_ZEROPAGE on private file mappings
2026-11-16 20:02 ` [PATCH v2 1/9] userfaultfd: use anonymous memory for UFFDIO_ZEROPAGE on private file mappings Tal Zussman
@ 2026-11-16 20:15 ` David Hildenbrand (Arm)
2026-11-16 20:16 ` David Hildenbrand (Arm)
1 sibling, 0 replies; 20+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-16 20:15 UTC (permalink / raw)
To: Tal Zussman, Andrew Morton, Mike Rapoport, Peter Xu,
James Houghton, Andrea Arcangeli, Suren Baghdasaryan,
Balbir Singh, Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, Pedro Falcato, Michal Hocko, Michał Mirosław,
Andrei Vagin, Muhammad Usama Anjum, Stephen Rothwell,
Arnd Bergmann, Zi Yan, Matthew Brost, Joshua Hahn, Byungchul Park,
Gregory Price, Ying Huang, Alistair Popple, Baolin Wang,
Nico Pache, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, Kiryl Shutsemau, Muchun Song, Oscar Salvador,
Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel, stable
On 11/16/26 21:02, Tal Zussman wrote:
> If the mm cannot map the shared zero page, UFFDIO_ZEROPAGE falls back to
> mfill_atomic_pte_zeroed_folio(), which allocates a zeroed folio and
> installs it through vma_uffd_ops(), no matter what kind of mapping it
> is. An s390 KVM guest with storage keys or protected virtualization is
> such an mm.
>
> For a MAP_PRIVATE shmem mapping, that adds the zeroed folio to the page
> cache of the file. That is wrong: a private mapping must never modify
> the file it maps, and the file might even be mapped from a read-only fd.
>
> The ioctl can also fail where it should succeed. A missing fault is only
> delivered for a hole in the file, but UFFDIO_ZEROPAGE can be called on
> an address that did not fault, or can race with a write to the file. If
> the file has a page at that offset by then, inserting the zeroed folio
> fails and the ioctl returns -EEXIST although nothing is mapped at the
> destination address.
>
> What we want is what UFFDIO_ZEROPAGE does on a private mapping when the
> shared zero page can be used, and what UFFDIO_COPY does: only check that
> nothing is mapped at the destination address, map zeroed memory there,
> and leave the page cache alone. The zeroed folio path did that before it
> was moved to vm_uffd_ops.
>
> To fix it, pick the ops in the common folio path, as
> mfill_atomic_pte_copy() did for UFFDIO_COPY: anon_uffd_ops for a private
> mapping, the ops of the VMA otherwise. That makes
> mfill_atomic_pte_copy() and mfill_atomic_pte_zeroed_folio() redundant,
> so remove them and rename __mfill_atomic_pte() to
> mfill_atomic_pte_folio().
>
> UFFDIO_ZEROPAGE no longer modifies the file, and only fails with -EEXIST
> if something is already mapped at the destination address.
>
> Fixes: f74991b4e383 ("shmem, userfaultfd: implement shmem uffd operations using vm_uffd_ops")
> Cc: stable@vger.kernel.org
> Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> Signed-off-by: Tal Zussman <tz2294@columbia.edu>
> ---
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 20+ messages in thread* Re: [PATCH v2 1/9] userfaultfd: use anonymous memory for UFFDIO_ZEROPAGE on private file mappings
2026-11-16 20:02 ` [PATCH v2 1/9] userfaultfd: use anonymous memory for UFFDIO_ZEROPAGE on private file mappings Tal Zussman
2026-11-16 20:15 ` David Hildenbrand (Arm)
@ 2026-11-16 20:16 ` David Hildenbrand (Arm)
1 sibling, 0 replies; 20+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-16 20:16 UTC (permalink / raw)
To: Tal Zussman, Andrew Morton, Mike Rapoport, Peter Xu,
James Houghton, Andrea Arcangeli, Suren Baghdasaryan,
Balbir Singh, Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, Pedro Falcato, Michal Hocko, Michał Mirosław,
Andrei Vagin, Muhammad Usama Anjum, Stephen Rothwell,
Arnd Bergmann, Zi Yan, Matthew Brost, Joshua Hahn, Byungchul Park,
Gregory Price, Ying Huang, Alistair Popple, Baolin Wang,
Nico Pache, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, Kiryl Shutsemau, Muchun Song, Oscar Salvador,
Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel, stable
On 11/16/26 21:02, Tal Zussman wrote:
> If the mm cannot map the shared zero page, UFFDIO_ZEROPAGE falls back to
> mfill_atomic_pte_zeroed_folio(), which allocates a zeroed folio and
> installs it through vma_uffd_ops(), no matter what kind of mapping it
> is. An s390 KVM guest with storage keys or protected virtualization is
> such an mm.
>
> For a MAP_PRIVATE shmem mapping, that adds the zeroed folio to the page
> cache of the file. That is wrong: a private mapping must never modify
> the file it maps, and the file might even be mapped from a read-only fd.
>
> The ioctl can also fail where it should succeed. A missing fault is only
> delivered for a hole in the file, but UFFDIO_ZEROPAGE can be called on
> an address that did not fault, or can race with a write to the file. If
> the file has a page at that offset by then, inserting the zeroed folio
> fails and the ioctl returns -EEXIST although nothing is mapped at the
> destination address.
>
> What we want is what UFFDIO_ZEROPAGE does on a private mapping when the
> shared zero page can be used, and what UFFDIO_COPY does: only check that
> nothing is mapped at the destination address, map zeroed memory there,
> and leave the page cache alone. The zeroed folio path did that before it
> was moved to vm_uffd_ops.
>
> To fix it, pick the ops in the common folio path, as
> mfill_atomic_pte_copy() did for UFFDIO_COPY: anon_uffd_ops for a private
> mapping, the ops of the VMA otherwise. That makes
> mfill_atomic_pte_copy() and mfill_atomic_pte_zeroed_folio() redundant,
> so remove them and rename __mfill_atomic_pte() to
> mfill_atomic_pte_folio().
>
> UFFDIO_ZEROPAGE no longer modifies the file, and only fails with -EEXIST
> if something is already mapped at the destination address.
>
> Fixes: f74991b4e383 ("shmem, userfaultfd: implement shmem uffd operations using vm_uffd_ops")
> Cc: stable@vger.kernel.org
> Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> Signed-off-by: Tal Zussman <tz2294@columbia.edu>
> ---
> mm/userfaultfd.c | 46 ++++++++++++++++------------------------------
> 1 file changed, 16 insertions(+), 30 deletions(-)
>
> diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
> index d65184d9a480..61a712e1f9f5 100644
> --- a/mm/userfaultfd.c
> +++ b/mm/userfaultfd.c
> @@ -531,15 +531,27 @@ static int mfill_copy_folio_retry(struct mfill_state *mfill_state,
> return 0;
> }
>
> -static int __mfill_atomic_pte(struct mfill_state *state,
> - const struct vm_uffd_ops *ops)
> +static int mfill_atomic_pte_folio(struct mfill_state *state)
> {
> + const struct vm_uffd_ops *ops = vma_uffd_ops(state->vma);
> unsigned long dst_addr = state->dst_addr;
> unsigned long src_addr = state->src_addr;
> uffd_flags_t flags = state->flags;
> struct folio *folio;
> int ret;
>
> + /*
> + * The normal page fault path for a MAP_PRIVATE mapping in a
> + * file-backed VMA will invoke the fault, fill the hole in the file and
> + * COW it right away. The result generates plain anonymous memory.
> + * So when we are asked to fill a hole in a MAP_PRIVATE mapping, we'll
> + * generate anonymous memory directly without actually filling the
> + * hole. For the MAP_PRIVATE case the robustness check only happens in
> + * the pagetable (to verify it's still none) and not in the page cache.
> + */
> + if (!(state->vma->vm_flags & VM_SHARED))
> + ops = &anon_uffd_ops;
Oh, but one comment: why not check for actual cow mappings?
vma_is_cow_mapping()
is your friend :)
--
Cheers,
David
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v2 2/9] userfaultfd: refuse UFFDIO_MOVE into a non-present huge destination PMD
2026-11-16 20:02 [PATCH v2 0/9] userfaultfd: assorted fixes (part 1) Tal Zussman
2026-11-16 20:02 ` [PATCH v2 1/9] userfaultfd: use anonymous memory for UFFDIO_ZEROPAGE on private file mappings Tal Zussman
@ 2026-11-16 20:02 ` Tal Zussman
2026-11-16 20:18 ` David Hildenbrand (Arm)
2026-11-16 20:02 ` [PATCH v2 3/9] fs/proc/task_mmu: downgrade writable migration entries in PAGEMAP_SCAN write-protect Tal Zussman
` (6 subsequent siblings)
8 siblings, 1 reply; 20+ messages in thread
From: Tal Zussman @ 2026-11-16 20:02 UTC (permalink / raw)
To: Andrew Morton, Mike Rapoport, Peter Xu, James Houghton,
Andrea Arcangeli, Suren Baghdasaryan, Balbir Singh,
Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka, Jann Horn,
Pedro Falcato, David Hildenbrand, Michal Hocko,
Michał Mirosław, Andrei Vagin, Muhammad Usama Anjum,
Stephen Rothwell, Arnd Bergmann, Zi Yan, Matthew Brost,
Joshua Hahn, Byungchul Park, Gregory Price, Ying Huang,
Alistair Popple, Baolin Wang, Nico Pache, Ryan Roberts, Dev Jain,
Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau, Muchun Song,
Oscar Salvador, Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel, stable, Tal Zussman
move_pages() refuses to move into a destination PMD that already maps a
THP, but checks for that with pmd_trans_huge(), which is only true for a
present THP. A device-private THP or a THP under migration is a PMD
softleaf entry and is not refused.
The destination PMD is then treated as if it pointed to a PTE table. It
is not empty, so pte_alloc() leaves it alone, and move_pages_ptes()
fails to map a PTE table from it and returns -EAGAIN. move_pages()
retries on -EAGAIN, with both VMA read locks held. If the source is a
THP, it is split first for nothing.
For a THP under migration, the loop ends once the migration finishes.
A device-private THP can remain in device memory indefinitely, so the
caller of UFFDIO_MOVE hangs until it receives a fatal signal, and
anything in the mm that needs to write-lock one of the VMAs blocks along
with it. This requires a driver that migrates THPs to device memory.
To fix it, check the destination with pmd_is_huge(), which is true for
a present THP and for a PMD softleaf entry. UFFDIO_MOVE now fails with
-EEXIST in these cases, as it already does for a present THP.
Fixes: adef440691ba ("userfaultfd: UFFDIO_MOVE uABI")
Fixes: a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
Cc: stable@vger.kernel.org
Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Tested-by: Gregory Price (Meta) <gourry@gourry.net>
Reviewed-by: Gregory Price (Meta) <gourry@gourry.net>
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
mm/userfaultfd.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 61a712e1f9f5..ad9d243dc64b 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -2021,12 +2021,13 @@ static ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,
dst_pmdval = pmdp_get_lockless(dst_pmd);
/*
- * If the dst_pmd is mapped as THP don't override it and just
- * be strict. If dst_pmd changes into TPH after this check, the
- * move_pages_huge_pmd() will detect the change and retry
- * while move_pages_pte() will detect the change and fail.
+ * If the dst_pmd is a huge leaf, present or not, don't
+ * override it and just be strict. If dst_pmd changes into a
+ * leaf after this check, move_pages_huge_pmd() will detect the
+ * change and retry while move_pages_ptes() will fail to map
+ * the PTE table and the next iteration will see the leaf.
*/
- if (unlikely(pmd_trans_huge(dst_pmdval))) {
+ if (unlikely(pmd_is_huge(dst_pmdval))) {
err = -EEXIST;
break;
}
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [PATCH v2 2/9] userfaultfd: refuse UFFDIO_MOVE into a non-present huge destination PMD
2026-11-16 20:02 ` [PATCH v2 2/9] userfaultfd: refuse UFFDIO_MOVE into a non-present huge destination PMD Tal Zussman
@ 2026-11-16 20:18 ` David Hildenbrand (Arm)
0 siblings, 0 replies; 20+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-16 20:18 UTC (permalink / raw)
To: Tal Zussman, Andrew Morton, Mike Rapoport, Peter Xu,
James Houghton, Andrea Arcangeli, Suren Baghdasaryan,
Balbir Singh, Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, Pedro Falcato, Michal Hocko, Michał Mirosław,
Andrei Vagin, Muhammad Usama Anjum, Stephen Rothwell,
Arnd Bergmann, Zi Yan, Matthew Brost, Joshua Hahn, Byungchul Park,
Gregory Price, Ying Huang, Alistair Popple, Baolin Wang,
Nico Pache, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, Kiryl Shutsemau, Muchun Song, Oscar Salvador,
Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel, stable
On 11/16/26 21:02, Tal Zussman wrote:
> move_pages() refuses to move into a destination PMD that already maps a
> THP, but checks for that with pmd_trans_huge(), which is only true for a
> present THP. A device-private THP or a THP under migration is a PMD
> softleaf entry and is not refused.
>
> The destination PMD is then treated as if it pointed to a PTE table. It
> is not empty, so pte_alloc() leaves it alone, and move_pages_ptes()
> fails to map a PTE table from it and returns -EAGAIN. move_pages()
> retries on -EAGAIN, with both VMA read locks held. If the source is a
> THP, it is split first for nothing.
>
> For a THP under migration, the loop ends once the migration finishes.
> A device-private THP can remain in device memory indefinitely, so the
> caller of UFFDIO_MOVE hangs until it receives a fatal signal, and
> anything in the mm that needs to write-lock one of the VMAs blocks along
> with it. This requires a driver that migrates THPs to device memory.
>
> To fix it, check the destination with pmd_is_huge(), which is true for
> a present THP and for a PMD softleaf entry. UFFDIO_MOVE now fails with
> -EEXIST in these cases, as it already does for a present THP.
>
> Fixes: adef440691ba ("userfaultfd: UFFDIO_MOVE uABI")
> Fixes: a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
> Cc: stable@vger.kernel.org
> Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> Tested-by: Gregory Price (Meta) <gourry@gourry.net>
> Reviewed-by: Gregory Price (Meta) <gourry@gourry.net>
> Signed-off-by: Tal Zussman <tz2294@columbia.edu>
> ---
> mm/userfaultfd.c | 11 ++++++-----
> 1 file changed, 6 insertions(+), 5 deletions(-)
>
> diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
> index 61a712e1f9f5..ad9d243dc64b 100644
> --- a/mm/userfaultfd.c
> +++ b/mm/userfaultfd.c
> @@ -2021,12 +2021,13 @@ static ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,
>
> dst_pmdval = pmdp_get_lockless(dst_pmd);
> /*
> - * If the dst_pmd is mapped as THP don't override it and just
> - * be strict. If dst_pmd changes into TPH after this check, the
> - * move_pages_huge_pmd() will detect the change and retry
> - * while move_pages_pte() will detect the change and fail.
> + * If the dst_pmd is a huge leaf, present or not, don't
> + * override it and just be strict. If dst_pmd changes into a
> + * leaf after this check, move_pages_huge_pmd() will detect the
> + * change and retry while move_pages_ptes() will fail to map
> + * the PTE table and the next iteration will see the leaf.
> */
> - if (unlikely(pmd_trans_huge(dst_pmdval))) {
> + if (unlikely(pmd_is_huge(dst_pmdval))) {
Insert the usual ranting about stupid thp helpers
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v2 3/9] fs/proc/task_mmu: downgrade writable migration entries in PAGEMAP_SCAN write-protect
2026-11-16 20:02 [PATCH v2 0/9] userfaultfd: assorted fixes (part 1) Tal Zussman
2026-11-16 20:02 ` [PATCH v2 1/9] userfaultfd: use anonymous memory for UFFDIO_ZEROPAGE on private file mappings Tal Zussman
2026-11-16 20:02 ` [PATCH v2 2/9] userfaultfd: refuse UFFDIO_MOVE into a non-present huge destination PMD Tal Zussman
@ 2026-11-16 20:02 ` Tal Zussman
2026-11-16 20:21 ` David Hildenbrand (Arm)
2026-11-16 20:02 ` [PATCH v2 4/9] mm/migrate: keep the uffd bit when restoring a writable migration entry Tal Zussman
` (5 subsequent siblings)
8 siblings, 1 reply; 20+ messages in thread
From: Tal Zussman @ 2026-11-16 20:02 UTC (permalink / raw)
To: Andrew Morton, Mike Rapoport, Peter Xu, James Houghton,
Andrea Arcangeli, Suren Baghdasaryan, Balbir Singh,
Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka, Jann Horn,
Pedro Falcato, David Hildenbrand, Michal Hocko,
Michał Mirosław, Andrei Vagin, Muhammad Usama Anjum,
Stephen Rothwell, Arnd Bergmann, Zi Yan, Matthew Brost,
Joshua Hahn, Byungchul Park, Gregory Price, Ying Huang,
Alistair Popple, Baolin Wang, Nico Pache, Ryan Roberts, Dev Jain,
Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau, Muchun Song,
Oscar Salvador, Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel, Breno Leitao, stable,
Tal Zussman
PAGEMAP_SCAN with PM_SCAN_WP_MATCHING write-protects pages in a VMA
registered for async uffd-wp, and sets the uffd bit on non-present
entries as they are. An entry with the uffd bit set must not be
writable, so that the page is write-protected once it is mapped again.
change_protection() therefore downgrades writable migration and
device-private entries to readable ones first, but PAGEMAP_SCAN does
not.
For a writable migration entry at the PTE level,
remove_migration_pte() restores a writable PTE without the uffd bit. So
if a page is under migration while PAGEMAP_SCAN write-protects it, the
write protection is lost. A user tracking writes with async uffd-wp
sees the page reported as written on the next scan although nothing
wrote to it.
With CONFIG_PAGE_TABLE_CHECK, setting the uffd bit on a writable
non-present PTE or PMD also triggers a warning.
To fix it, downgrade writable migration and device-private entries
before setting the uffd bit, as change_protection() does, at the PTE,
PMD, and hugetlb levels.
Add softleaf_is_writable() and softleaf_mkreadable() to leafops.h for
the downgrade. The open-coded copies in change_protection() can be
converted to them separately.
Fixes: 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")
Reported-by: Breno Leitao <leitao@debian.org>
Closes: https://lore.kernel.org/all/avNEgmvYdoukt6MS@gmail.com/
Cc: stable@vger.kernel.org
Tested-by: Gregory Price (Meta) <gourry@gourry.net>
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/proc/task_mmu.c | 40 +++++++++++++++++++++++++++++++++++---
include/linux/leafops.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 88 insertions(+), 3 deletions(-)
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 164b4efc5e09..ee7f509d376e 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -2405,6 +2405,25 @@ static unsigned long pagemap_page_category(struct pagemap_scan_private *p,
return categories;
}
+/*
+ * An armed entry must not come back writable, so downgrade a writable
+ * migration or device-private entry before giving it the bit, as
+ * change_softleaf_pte() does.
+ */
+static pte_t set_uffd_wp_softleaf_pte(pte_t pte)
+{
+ softleaf_t entry = softleaf_from_pte(pte);
+ pte_t newpte;
+
+ if (!softleaf_is_writable(entry))
+ return pte_swp_mkuffd(pte);
+
+ newpte = softleaf_to_pte(softleaf_mkreadable(entry));
+ if (pte_swp_soft_dirty(pte))
+ newpte = pte_swp_mksoft_dirty(newpte);
+ return pte_swp_mkuffd(newpte);
+}
+
static void make_uffd_wp_pte(struct vm_area_struct *vma,
unsigned long addr, pte_t *pte, pte_t ptent)
{
@@ -2418,7 +2437,7 @@ static void make_uffd_wp_pte(struct vm_area_struct *vma,
set_pte_at(vma->vm_mm, addr, pte,
make_pte_marker(PTE_MARKER_UFFD_WP));
} else {
- ptent = pte_swp_mkuffd(ptent);
+ ptent = set_uffd_wp_softleaf_pte(ptent);
set_pte_at(vma->vm_mm, addr, pte, ptent);
}
}
@@ -2477,6 +2496,21 @@ static unsigned long pagemap_thp_category(struct pagemap_scan_private *p,
return categories;
}
+/* Same as set_uffd_wp_softleaf_pte(), for a PMD. */
+static pmd_t set_uffd_wp_softleaf_pmd(pmd_t pmd)
+{
+ softleaf_t entry = softleaf_from_pmd(pmd);
+ pmd_t newpmd;
+
+ if (!softleaf_is_writable(entry))
+ return pmd_swp_mkuffd(pmd);
+
+ newpmd = softleaf_to_pmd(softleaf_mkreadable(entry));
+ if (pmd_swp_soft_dirty(pmd))
+ newpmd = pmd_swp_mksoft_dirty(newpmd);
+ return pmd_swp_mkuffd(newpmd);
+}
+
static void make_uffd_wp_pmd(struct vm_area_struct *vma,
unsigned long addr, pmd_t *pmdp)
{
@@ -2487,7 +2521,7 @@ static void make_uffd_wp_pmd(struct vm_area_struct *vma,
pmd = pmd_mkuffd(old);
set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
} else if (pmd_is_migration_entry(pmd)) {
- pmd = pmd_swp_mkuffd(pmd);
+ pmd = set_uffd_wp_softleaf_pmd(pmd);
set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
}
}
@@ -2557,7 +2591,7 @@ static void make_uffd_wp_huge_pte(struct vm_area_struct *vma,
if (softleaf_is_migration(entry)) {
set_huge_pte_at(vma->vm_mm, addr, ptep,
- pte_swp_mkuffd(ptent), psize);
+ set_uffd_wp_softleaf_pte(ptent), psize);
} else {
pte_t old_pte, new_pte;
diff --git a/include/linux/leafops.h b/include/linux/leafops.h
index 4c1476ae3234..4c016fc28690 100644
--- a/include/linux/leafops.h
+++ b/include/linux/leafops.h
@@ -434,6 +434,57 @@ static inline struct folio *softleaf_to_folio(softleaf_t entry)
return folio;
}
+/**
+ * softleaf_is_writable() - Is this leaf entry writable?
+ * @entry: Leaf entry.
+ *
+ * Returns: true for a writable migration or device private entry.
+ */
+static inline bool softleaf_is_writable(softleaf_t entry)
+{
+ return softleaf_is_migration_write(entry) ||
+ softleaf_is_device_private_write(entry);
+}
+
+/**
+ * softleaf_migration_mkreadable() - Downgrade a writable migration entry to a
+ * readable one.
+ * @entry: Leaf entry, softleaf_is_migration_write(@entry) must return true.
+ *
+ * Returns: A readable migration entry for the same PFN, which is exclusive if
+ * the folio is anonymous.
+ */
+static inline softleaf_t softleaf_migration_mkreadable(softleaf_t entry)
+{
+ const pgoff_t offset = swp_offset(entry);
+
+ VM_WARN_ON_ONCE(!softleaf_is_migration_write(entry));
+
+ /* A writable anon migration entry implies AnonExclusive. */
+ if (folio_test_anon(softleaf_to_folio(entry)))
+ return make_readable_exclusive_migration_entry(offset);
+ return make_readable_migration_entry(offset);
+}
+
+/**
+ * softleaf_mkreadable() - Downgrade a writable leaf entry to a readable one.
+ * @entry: Leaf entry.
+ *
+ * Returns: A readable entry if @entry is a writable migration or device
+ * private entry, otherwise @entry unchanged.
+ */
+static inline softleaf_t softleaf_mkreadable(softleaf_t entry)
+{
+ switch (softleaf_type(entry)) {
+ case SOFTLEAF_DEVICE_PRIVATE_WRITE:
+ return make_readable_device_private_entry(swp_offset(entry));
+ case SOFTLEAF_MIGRATION_WRITE:
+ return softleaf_migration_mkreadable(entry);
+ default:
+ return entry;
+ }
+}
+
/**
* softleaf_is_poison_marker() - Is this leaf entry a poison marker?
* @entry: Leaf entry.
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [PATCH v2 3/9] fs/proc/task_mmu: downgrade writable migration entries in PAGEMAP_SCAN write-protect
2026-11-16 20:02 ` [PATCH v2 3/9] fs/proc/task_mmu: downgrade writable migration entries in PAGEMAP_SCAN write-protect Tal Zussman
@ 2026-11-16 20:21 ` David Hildenbrand (Arm)
0 siblings, 0 replies; 20+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-16 20:21 UTC (permalink / raw)
To: Tal Zussman, Andrew Morton, Mike Rapoport, Peter Xu,
James Houghton, Andrea Arcangeli, Suren Baghdasaryan,
Balbir Singh, Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, Pedro Falcato, Michal Hocko, Michał Mirosław,
Andrei Vagin, Muhammad Usama Anjum, Stephen Rothwell,
Arnd Bergmann, Zi Yan, Matthew Brost, Joshua Hahn, Byungchul Park,
Gregory Price, Ying Huang, Alistair Popple, Baolin Wang,
Nico Pache, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, Kiryl Shutsemau, Muchun Song, Oscar Salvador,
Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel, Breno Leitao, stable
On 11/16/26 21:02, Tal Zussman wrote:
> PAGEMAP_SCAN with PM_SCAN_WP_MATCHING write-protects pages in a VMA
Today on "things that should have never been merged": PAGEMAP_SCAN and
PM_SCAN_WP_MATCHING
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v2 4/9] mm/migrate: keep the uffd bit when restoring a writable migration entry
2026-11-16 20:02 [PATCH v2 0/9] userfaultfd: assorted fixes (part 1) Tal Zussman
` (2 preceding siblings ...)
2026-11-16 20:02 ` [PATCH v2 3/9] fs/proc/task_mmu: downgrade writable migration entries in PAGEMAP_SCAN write-protect Tal Zussman
@ 2026-11-16 20:02 ` Tal Zussman
2026-11-16 20:23 ` David Hildenbrand (Arm)
2026-11-16 20:02 ` [PATCH v2 5/9] fs/proc/task_mmu: write-protect device-private THPs in PAGEMAP_SCAN Tal Zussman
` (4 subsequent siblings)
8 siblings, 1 reply; 20+ messages in thread
From: Tal Zussman @ 2026-11-16 20:02 UTC (permalink / raw)
To: Andrew Morton, Mike Rapoport, Peter Xu, James Houghton,
Andrea Arcangeli, Suren Baghdasaryan, Balbir Singh,
Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka, Jann Horn,
Pedro Falcato, David Hildenbrand, Michal Hocko,
Michał Mirosław, Andrei Vagin, Muhammad Usama Anjum,
Stephen Rothwell, Arnd Bergmann, Zi Yan, Matthew Brost,
Joshua Hahn, Byungchul Park, Gregory Price, Ying Huang,
Alistair Popple, Baolin Wang, Nico Pache, Ryan Roberts, Dev Jain,
Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau, Muchun Song,
Oscar Salvador, Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel, Tal Zussman
remove_migration_pte() makes the restored PTE writable for a writable
migration entry, and only looks at the uffd bit of the entry otherwise.
A writable migration entry that has the uffd bit set is therefore
restored as a writable PTE without the uffd bit, and the page is no
longer write-protected.
An entry with the uffd bit set must be restored write-protected, whether
the entry is writable or not. remove_migration_pmd() gets this right: it
makes the PMD writable and sets the uffd bit as two independent steps,
and pmd_mkuffd() write-protects the result. To fix it, do the same at
the PTE level, where pte_mkuffd() write-protects the PTE as well.
The only known path that created such entries is PAGEMAP_SCAN. With
PM_SCAN_WP_MATCHING, it write-protects the pages it scans, and it set
the uffd bit on a writable migration entry without downgrading it. Now
that PAGEMAP_SCAN downgrades them like change_protection() does, we
should no longer see them, so this change alone should not be visible to
userspace. If another path ever creates a writable migration entry with
the uffd bit set, the page now stays write-protected instead of losing
the uffd bit.
Fixes: 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")
Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Tested-by: Gregory Price (Meta) <gourry@gourry.net>
Reviewed-by: Gregory Price (Meta) <gourry@gourry.net>
Reviewed-by: Alistair Popple <apopple@nvidia.com>
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
mm/migrate.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mm/migrate.c b/mm/migrate.c
index 28a8d7734735..d200f64b5f98 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -399,7 +399,7 @@ static bool remove_migration_pte(struct folio *folio,
if (softleaf_is_migration_write(entry))
pte = pte_mkwrite(pte, vma);
- else if (pte_swp_uffd(old_pte))
+ if (pte_swp_uffd(old_pte))
pte = pte_mkuffd(pte);
/* See do_swap_page(): restore PAGE_NONE for RWP */
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [PATCH v2 4/9] mm/migrate: keep the uffd bit when restoring a writable migration entry
2026-11-16 20:02 ` [PATCH v2 4/9] mm/migrate: keep the uffd bit when restoring a writable migration entry Tal Zussman
@ 2026-11-16 20:23 ` David Hildenbrand (Arm)
0 siblings, 0 replies; 20+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-16 20:23 UTC (permalink / raw)
To: Tal Zussman, Andrew Morton, Mike Rapoport, Peter Xu,
James Houghton, Andrea Arcangeli, Suren Baghdasaryan,
Balbir Singh, Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, Pedro Falcato, Michal Hocko, Michał Mirosław,
Andrei Vagin, Muhammad Usama Anjum, Stephen Rothwell,
Arnd Bergmann, Zi Yan, Matthew Brost, Joshua Hahn, Byungchul Park,
Gregory Price, Ying Huang, Alistair Popple, Baolin Wang,
Nico Pache, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, Kiryl Shutsemau, Muchun Song, Oscar Salvador,
Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel
On 11/16/26 21:02, Tal Zussman wrote:
> remove_migration_pte() makes the restored PTE writable for a writable
> migration entry, and only looks at the uffd bit of the entry otherwise.
> A writable migration entry that has the uffd bit set is therefore
> restored as a writable PTE without the uffd bit, and the page is no
> longer write-protected.
>
> An entry with the uffd bit set must be restored write-protected, whether
> the entry is writable or not. remove_migration_pmd() gets this right: it
> makes the PMD writable and sets the uffd bit as two independent steps,
> and pmd_mkuffd() write-protects the result. To fix it, do the same at
> the PTE level, where pte_mkuffd() write-protects the PTE as well.
>
> The only known path that created such entries is PAGEMAP_SCAN. With
> PM_SCAN_WP_MATCHING, it write-protects the pages it scans, and it set
> the uffd bit on a writable migration entry without downgrading it. Now
> that PAGEMAP_SCAN downgrades them like change_protection() does, we
> should no longer see them, so this change alone should not be visible to
> userspace. If another path ever creates a writable migration entry with
> the uffd bit set, the page now stays write-protected instead of losing
> the uffd bit.
I am severely confused. If it was properly fixed, why should we do the below?
You should WARN if we get the uffd-wp bit set on something that is writable, not
silently swallow it.
--
Cheers,
David
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v2 5/9] fs/proc/task_mmu: write-protect device-private THPs in PAGEMAP_SCAN
2026-11-16 20:02 [PATCH v2 0/9] userfaultfd: assorted fixes (part 1) Tal Zussman
` (3 preceding siblings ...)
2026-11-16 20:02 ` [PATCH v2 4/9] mm/migrate: keep the uffd bit when restoring a writable migration entry Tal Zussman
@ 2026-11-16 20:02 ` Tal Zussman
2026-11-16 20:26 ` David Hildenbrand (Arm)
2026-11-16 20:02 ` [PATCH v2 6/9] mm/huge_memory: build the moved huge zero PMD from the destination VMA Tal Zussman
` (3 subsequent siblings)
8 siblings, 1 reply; 20+ messages in thread
From: Tal Zussman @ 2026-11-16 20:02 UTC (permalink / raw)
To: Andrew Morton, Mike Rapoport, Peter Xu, James Houghton,
Andrea Arcangeli, Suren Baghdasaryan, Balbir Singh,
Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka, Jann Horn,
Pedro Falcato, David Hildenbrand, Michal Hocko,
Michał Mirosław, Andrei Vagin, Muhammad Usama Anjum,
Stephen Rothwell, Arnd Bergmann, Zi Yan, Matthew Brost,
Joshua Hahn, Byungchul Park, Gregory Price, Ying Huang,
Alistair Popple, Baolin Wang, Nico Pache, Ryan Roberts, Dev Jain,
Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau, Muchun Song,
Oscar Salvador, Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel, Tal Zussman
PAGEMAP_SCAN with PM_SCAN_WP_MATCHING write-protects a huge PMD through
make_uffd_wp_pmd(), which handles a present THP and a migration entry
but does nothing for a device-private THP. The PMD never gets the uffd
bit.
pagemap_thp_category() reports a non-present PMD without the uffd bit as
written. A user tracking writes with async uffd-wp therefore sees every
page of a device-private THP as written on every scan, whether it was
written to or not. This requires a driver that migrates THPs to device
memory.
A device-private PMD must be write-protected like any other huge PMD. To
fix it, pass any valid PMD softleaf entry to set_uffd_wp_softleaf_pmd(),
not only a migration entry. It downgrades a writable entry and sets the
uffd bit. When the THP migrates back, set_pmd_migration_entry() carries
the uffd bit over to the migration entry, so the write protection is
kept.
Fixes: a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
Tested-by: Gregory Price (Meta) <gourry@gourry.net>
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
fs/proc/task_mmu.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index ee7f509d376e..013eeea7d1a3 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -2520,7 +2520,7 @@ static void make_uffd_wp_pmd(struct vm_area_struct *vma,
old = pmdp_invalidate_ad(vma, addr, pmdp);
pmd = pmd_mkuffd(old);
set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
- } else if (pmd_is_migration_entry(pmd)) {
+ } else if (pmd_is_valid_softleaf(pmd)) {
pmd = set_uffd_wp_softleaf_pmd(pmd);
set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
}
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [PATCH v2 5/9] fs/proc/task_mmu: write-protect device-private THPs in PAGEMAP_SCAN
2026-11-16 20:02 ` [PATCH v2 5/9] fs/proc/task_mmu: write-protect device-private THPs in PAGEMAP_SCAN Tal Zussman
@ 2026-11-16 20:26 ` David Hildenbrand (Arm)
0 siblings, 0 replies; 20+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-16 20:26 UTC (permalink / raw)
To: Tal Zussman, Andrew Morton, Mike Rapoport, Peter Xu,
James Houghton, Andrea Arcangeli, Suren Baghdasaryan,
Balbir Singh, Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, Pedro Falcato, Michal Hocko, Michał Mirosław,
Andrei Vagin, Muhammad Usama Anjum, Stephen Rothwell,
Arnd Bergmann, Zi Yan, Matthew Brost, Joshua Hahn, Byungchul Park,
Gregory Price, Ying Huang, Alistair Popple, Baolin Wang,
Nico Pache, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, Kiryl Shutsemau, Muchun Song, Oscar Salvador,
Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel
On 11/16/26 21:02, Tal Zussman wrote:
> PAGEMAP_SCAN with PM_SCAN_WP_MATCHING write-protects a huge PMD through
> make_uffd_wp_pmd(), which handles a present THP and a migration entry
> but does nothing for a device-private THP. The PMD never gets the uffd
> bit.
>
> pagemap_thp_category() reports a non-present PMD without the uffd bit as
> written. A user tracking writes with async uffd-wp therefore sees every
> page of a device-private THP as written on every scan, whether it was
> written to or not. This requires a driver that migrates THPs to device
> memory.
>
> A device-private PMD must be write-protected like any other huge PMD. To
> fix it, pass any valid PMD softleaf entry to set_uffd_wp_softleaf_pmd(),
> not only a migration entry. It downgrades a writable entry and sets the
> uffd bit. When the THP migrates back, set_pmd_migration_entry() carries
> the uffd bit over to the migration entry, so the write protection is
> kept.
>
> Fixes: a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
> Tested-by: Gregory Price (Meta) <gourry@gourry.net>
> Signed-off-by: Tal Zussman <tz2294@columbia.edu>
> ---
> fs/proc/task_mmu.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
> index ee7f509d376e..013eeea7d1a3 100644
> --- a/fs/proc/task_mmu.c
> +++ b/fs/proc/task_mmu.c
> @@ -2520,7 +2520,7 @@ static void make_uffd_wp_pmd(struct vm_area_struct *vma,
> old = pmdp_invalidate_ad(vma, addr, pmdp);
> pmd = pmd_mkuffd(old);
> set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
> - } else if (pmd_is_migration_entry(pmd)) {
> + } else if (pmd_is_valid_softleaf(pmd)) {
> pmd = set_uffd_wp_softleaf_pmd(pmd);
> set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
> }
>
This really should be squashed int #3 I think. Or what's the reason for keeping
this separate?
--
Cheers,
David
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v2 6/9] mm/huge_memory: build the moved huge zero PMD from the destination VMA
2026-11-16 20:02 [PATCH v2 0/9] userfaultfd: assorted fixes (part 1) Tal Zussman
` (4 preceding siblings ...)
2026-11-16 20:02 ` [PATCH v2 5/9] fs/proc/task_mmu: write-protect device-private THPs in PAGEMAP_SCAN Tal Zussman
@ 2026-11-16 20:02 ` Tal Zussman
2026-11-16 20:32 ` David Hildenbrand (Arm)
2026-11-16 20:02 ` [PATCH v2 7/9] mm/mremap: resolve uffd state when the context is dropped, not during the move Tal Zussman
` (2 subsequent siblings)
8 siblings, 1 reply; 20+ messages in thread
From: Tal Zussman @ 2026-11-16 20:02 UTC (permalink / raw)
To: Andrew Morton, Mike Rapoport, Peter Xu, James Houghton,
Andrea Arcangeli, Suren Baghdasaryan, Balbir Singh,
Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka, Jann Horn,
Pedro Falcato, David Hildenbrand, Michal Hocko,
Michał Mirosław, Andrei Vagin, Muhammad Usama Anjum,
Stephen Rothwell, Arnd Bergmann, Zi Yan, Matthew Brost,
Joshua Hahn, Byungchul Park, Gregory Price, Ying Huang,
Alistair Popple, Baolin Wang, Nico Pache, Ryan Roberts, Dev Jain,
Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau, Muchun Song,
Oscar Salvador, Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel, Tal Zussman
move_pages_huge_pmd() builds the destination PMD for the huge zero page
by copying the source PMD and clearing its uffd bit, so the destination
keeps the protection of the source.
A source VMA registered for RWP can map the huge zero page with a
PAGE_NONE PMD, and UFFDIO_MOVE does not check how the source is
registered. The destination then gets a PAGE_NONE PMD without the uffd
bit, although nothing asked for that in the destination VMA.
The next access fixes it up through the NUMA hinting fault path. Until
then, the first access to the destination takes a spurious fault and GUP
has to take the fault path. The impact on userspace is small.
The moved entry must have the protection of the destination VMA. To fix
it, build the entry from the vm_page_prot of the destination VMA, as
move_zeropage_pte() does for the small zero page, and keep marking it
soft-dirty. A destination VMA registered for RWP still gets its PMD
re-armed afterwards.
Fixes: 763f43865fc4 ("mm: preserve RWP marker across PTE rewrites")
Reviewed-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
Tested-by: Gregory Price (Meta) <gourry@gourry.net>
Reviewed-by: Gregory Price (Meta) <gourry@gourry.net>
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
mm/huge_memory.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 5e2629602221..a2b1ac11e7dd 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -3003,9 +3003,9 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm
/* Follow mremap() behavior and treat the entry dirty after the move */
_dst_pmd = pmd_mkwrite(pmd_mkdirty(_dst_pmd), dst_vma);
} else {
- src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd);
- _dst_pmd = move_soft_dirty_pmd(src_pmdval);
- _dst_pmd = clear_uffd_wp_pmd(_dst_pmd);
+ pmdp_huge_clear_flush(src_vma, src_addr, src_pmd);
+ _dst_pmd = folio_mk_pmd(huge_zero_folio, dst_vma->vm_page_prot);
+ _dst_pmd = move_soft_dirty_pmd(pmd_mkspecial(_dst_pmd));
}
/* Re-arm RWP on the moved PMD if dst_vma is RWP-registered. */
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [PATCH v2 6/9] mm/huge_memory: build the moved huge zero PMD from the destination VMA
2026-11-16 20:02 ` [PATCH v2 6/9] mm/huge_memory: build the moved huge zero PMD from the destination VMA Tal Zussman
@ 2026-11-16 20:32 ` David Hildenbrand (Arm)
0 siblings, 0 replies; 20+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-16 20:32 UTC (permalink / raw)
To: Tal Zussman, Andrew Morton, Mike Rapoport, Peter Xu,
James Houghton, Andrea Arcangeli, Suren Baghdasaryan,
Balbir Singh, Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, Pedro Falcato, Michal Hocko, Michał Mirosław,
Andrei Vagin, Muhammad Usama Anjum, Stephen Rothwell,
Arnd Bergmann, Zi Yan, Matthew Brost, Joshua Hahn, Byungchul Park,
Gregory Price, Ying Huang, Alistair Popple, Baolin Wang,
Nico Pache, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, Kiryl Shutsemau, Muchun Song, Oscar Salvador,
Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel
On 11/16/26 21:02, Tal Zussman wrote:
> move_pages_huge_pmd() builds the destination PMD for the huge zero page
> by copying the source PMD and clearing its uffd bit, so the destination
> keeps the protection of the source.
>
> A source VMA registered for RWP can map the huge zero page with a
> PAGE_NONE PMD, and UFFDIO_MOVE does not check how the source is
> registered. The destination then gets a PAGE_NONE PMD without the uffd
> bit, although nothing asked for that in the destination VMA.
>
> The next access fixes it up through the NUMA hinting fault path. Until
> then, the first access to the destination takes a spurious fault and GUP
> has to take the fault path. The impact on userspace is small.
>
> The moved entry must have the protection of the destination VMA. To fix
> it, build the entry from the vm_page_prot of the destination VMA, as
> move_zeropage_pte() does for the small zero page, and keep marking it
> soft-dirty. A destination VMA registered for RWP still gets its PMD
> re-armed afterwards.
>
> Fixes: 763f43865fc4 ("mm: preserve RWP marker across PTE rewrites")
> Reviewed-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> Tested-by: Gregory Price (Meta) <gourry@gourry.net>
> Reviewed-by: Gregory Price (Meta) <gourry@gourry.net>
> Signed-off-by: Tal Zussman <tz2294@columbia.edu>
> ---
> mm/huge_memory.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 5e2629602221..a2b1ac11e7dd 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -3003,9 +3003,9 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm
> /* Follow mremap() behavior and treat the entry dirty after the move */
> _dst_pmd = pmd_mkwrite(pmd_mkdirty(_dst_pmd), dst_vma);
> } else {
> - src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd);
> - _dst_pmd = move_soft_dirty_pmd(src_pmdval);
> - _dst_pmd = clear_uffd_wp_pmd(_dst_pmd);
> + pmdp_huge_clear_flush(src_vma, src_addr, src_pmd);
> + _dst_pmd = folio_mk_pmd(huge_zero_folio, dst_vma->vm_page_prot);
> + _dst_pmd = move_soft_dirty_pmd(pmd_mkspecial(_dst_pmd));
Yeah, mostly what we do in set_huge_zero_folio().
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v2 7/9] mm/mremap: resolve uffd state when the context is dropped, not during the move
2026-11-16 20:02 [PATCH v2 0/9] userfaultfd: assorted fixes (part 1) Tal Zussman
` (5 preceding siblings ...)
2026-11-16 20:02 ` [PATCH v2 6/9] mm/huge_memory: build the moved huge zero PMD from the destination VMA Tal Zussman
@ 2026-11-16 20:02 ` Tal Zussman
2026-11-16 20:41 ` David Hildenbrand (Arm)
2026-11-16 20:02 ` [PATCH v2 8/9] mm/migrate_device: do not migrate a uffd-protected zero page Tal Zussman
2026-11-16 20:02 ` [PATCH v2 9/9] mm/userfaultfd: check hugetlb end alignment in UFFDIO_UNREGISTER pre-scan Tal Zussman
8 siblings, 1 reply; 20+ messages in thread
From: Tal Zussman @ 2026-11-16 20:02 UTC (permalink / raw)
To: Andrew Morton, Mike Rapoport, Peter Xu, James Houghton,
Andrea Arcangeli, Suren Baghdasaryan, Balbir Singh,
Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka, Jann Horn,
Pedro Falcato, David Hildenbrand, Michal Hocko,
Michał Mirosław, Andrei Vagin, Muhammad Usama Anjum,
Stephen Rothwell, Arnd Bergmann, Zi Yan, Matthew Brost,
Joshua Hahn, Byungchul Park, Gregory Price, Ying Huang,
Alistair Popple, Baolin Wang, Nico Pache, Ryan Roberts, Dev Jain,
Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau, Muchun Song,
Oscar Salvador, Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel, Tal Zussman
Without UFFD_FEATURE_EVENT_REMAP, the new VMA of an mremap() is not
registered with userfaultfd, so the uffd state of its entries is cleared
while they are moved. If the move fails halfway, the entries are moved
back into the old VMA, which is still registered, without their uffd
state. Writes and accesses to them are then no longer reported.
To fix it, leave the entries alone during the move and clear the uffd
state of the new VMA in mremap_userfaultfd_prep(), right before dropping
the context, like unregistering does. This walks the page tables of the
new VMA a second time.
This is done with userfaultfd_clear_ptes(), which also makes the PTEs
writable again where that is safe. That removes a spurious write fault
per protected page on private anonymous memory, but adds spurious write
faults on shmem and hugetlb, as change_protection() makes writable pages
that were never protected read-only. UFFDIO_UNREGISTER and
UFFDIO_WRITEPROTECT already behave this way. This will be fixed in
change_protection() separately.
The entries no longer need per-entry handling during the move, so drop
uffd_supports_page_table_move() and clear_uffd_wp_pmd(). Whole page
tables of these VMAs can be moved again.
Fixes: 0cef0bb836e3 ("mm: clear uffd-wp PTE/PMD state on mremap()")
Fixes: 763f43865fc4 ("mm: preserve RWP marker across PTE rewrites")
Reviewed-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Tested-by: Gregory Price (Meta) <gourry@gourry.net>
Reviewed-by: Gregory Price (Meta) <gourry@gourry.net>
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
mm/huge_memory.c | 25 -------------------------
mm/hugetlb.c | 27 +--------------------------
mm/mremap.c | 46 +---------------------------------------------
mm/userfaultfd.c | 1 +
4 files changed, 3 insertions(+), 96 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index a2b1ac11e7dd..e994ca58cc70 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -2638,18 +2638,6 @@ static pmd_t move_soft_dirty_pmd(pmd_t pmd)
return pmd;
}
-static pmd_t clear_uffd_wp_pmd(pmd_t pmd)
-{
- if (pmd_none(pmd))
- return pmd;
- if (pmd_present(pmd))
- pmd = pmd_clear_uffd(pmd);
- else
- pmd = pmd_swp_clear_uffd(pmd);
-
- return pmd;
-}
-
bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd)
{
@@ -2685,19 +2673,6 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
}
pmd = move_soft_dirty_pmd(pmd);
- if (vma_has_uffd_without_event_remap(vma)) {
- /*
- * See __copy_present_ptes(): normalise the RWP marker
- * so the destination starts accessible instead of
- * taking a numa-hinting fault on first access. Only the
- * marker (protnone + uffd) needs it; leave other present
- * PMDs in the VMA untouched.
- */
- if (pmd_present(pmd) && userfaultfd_rwp(vma) &&
- pmd_uffd(pmd))
- pmd = pmd_modify(pmd, vma->vm_page_prot);
- pmd = clear_uffd_wp_pmd(pmd);
- }
set_pmd_at(mm, new_addr, new_pmd, pmd);
if (force_flush)
flush_pmd_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 88c9aa700533..b491742702f3 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -5195,7 +5195,6 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,
unsigned long new_addr, pte_t *src_pte, pte_t *dst_pte,
unsigned long sz)
{
- bool need_clear_uffd_wp = vma_has_uffd_without_event_remap(vma);
struct hstate *h = hstate_vma(vma);
struct mm_struct *mm = vma->vm_mm;
spinlock_t *src_ptl, *dst_ptl;
@@ -5206,31 +5205,7 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,
double_pt_lock(dst_ptl, src_ptl);
pte = huge_ptep_get_and_clear(mm, old_addr, src_pte, sz);
-
- if (need_clear_uffd_wp && pte_is_uffd_wp_marker(pte)) {
- huge_pte_clear(mm, new_addr, dst_pte, sz);
- } else {
- if (need_clear_uffd_wp) {
- if (pte_present(pte)) {
- /*
- * See __copy_present_ptes(): normalise the RWP
- * marker so the destination starts accessible
- * instead of taking a numa-hinting fault on
- * first access. Only the marker (protnone + uffd)
- * needs it; leave other present PTEs untouched.
- */
- if (userfaultfd_rwp(vma) && huge_pte_uffd(pte)) {
- pte = huge_pte_modify(pte, vma->vm_page_prot);
- pte = arch_make_huge_pte(pte, huge_page_shift(h),
- vma->vm_flags);
- }
- pte = huge_pte_clear_uffd(pte);
- } else {
- pte = pte_swp_clear_uffd(pte);
- }
- }
- set_huge_pte_at(mm, new_addr, dst_pte, pte, sz);
- }
+ set_huge_pte_at(mm, new_addr, dst_pte, pte, sz);
double_pt_unlock(dst_ptl, src_ptl);
}
diff --git a/mm/mremap.c b/mm/mremap.c
index fff0465f5e06..4564416a6994 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -198,7 +198,6 @@ static int move_ptes(struct pagetable_move_control *pmc,
unsigned long extent, pmd_t *old_pmd, pmd_t *new_pmd)
{
struct vm_area_struct *vma = pmc->old;
- bool need_clear_uffd_wp = vma_has_uffd_without_event_remap(vma);
struct mm_struct *mm = vma->vm_mm;
pte_t *old_ptep, *new_ptep;
pte_t old_pte, pte;
@@ -290,27 +289,7 @@ static int move_ptes(struct pagetable_move_control *pmc,
pte = get_and_clear_ptes(mm, old_addr, old_ptep, nr_ptes);
pte = move_pte(pte, old_addr, new_addr);
pte = move_soft_dirty_pte(pte);
-
- if (need_clear_uffd_wp && pte_is_uffd_wp_marker(pte))
- pte_clear(mm, new_addr, new_ptep);
- else {
- if (need_clear_uffd_wp) {
- if (pte_present(pte)) {
- /*
- * See __copy_present_ptes(): normalise
- * RWP PTEs so the destination starts
- * accessible instead of taking a
- * numa-hinting fault on first access.
- */
- if (userfaultfd_rwp(vma) && pte_uffd(pte))
- pte = pte_modify(pte, vma->vm_page_prot);
- pte = pte_clear_uffd(pte);
- } else {
- pte = pte_swp_clear_uffd(pte);
- }
- }
- set_ptes(mm, new_addr, new_ptep, pte, nr_ptes);
- }
+ set_ptes(mm, new_addr, new_ptep, pte, nr_ptes);
}
lazy_mmu_mode_disable();
@@ -334,25 +313,6 @@ static inline bool arch_supports_page_table_move(void)
}
#endif
-static inline bool uffd_supports_page_table_move(struct pagetable_move_control *pmc)
-{
- /*
- * If we are moving a VMA that has uffd-wp registered but with
- * remap events disabled (new VMA will not be registered with uffd), we
- * need to ensure that the uffd-wp state is cleared from all pgtables.
- * This means recursing into lower page tables in move_page_tables().
- *
- * We might get called with VMAs reversed when recovering from a
- * failed page table move. In that case, the
- * "old"-but-actually-"originally new" VMA during recovery will not have
- * a uffd context. Recursing into lower page tables during the original
- * move but not during the recovery move will cause trouble, because we
- * run into already-existing page tables. So check both VMAs.
- */
- return !vma_has_uffd_without_event_remap(pmc->old) &&
- !vma_has_uffd_without_event_remap(pmc->new);
-}
-
#ifdef CONFIG_HAVE_MOVE_PMD
static bool move_normal_pmd(struct pagetable_move_control *pmc,
pmd_t *old_pmd, pmd_t *new_pmd)
@@ -365,8 +325,6 @@ static bool move_normal_pmd(struct pagetable_move_control *pmc,
if (!arch_supports_page_table_move())
return false;
- if (!uffd_supports_page_table_move(pmc))
- return false;
/*
* The destination pmd shouldn't be established, free_pgtables()
* should have released it.
@@ -434,8 +392,6 @@ static bool move_normal_pud(struct pagetable_move_control *pmc,
if (!arch_supports_page_table_move())
return false;
- if (!uffd_supports_page_table_move(pmc))
- return false;
/*
* The destination pud shouldn't be established, free_pgtables()
* should have released it.
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index ad9d243dc64b..bf42fabb5afd 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -3161,6 +3161,7 @@ void mremap_userfaultfd_prep(struct vm_area_struct *vma,
up_write(&ctx->map_changing_lock);
} else {
/* Drop uffd context if remap feature not enabled */
+ userfaultfd_clear_ptes(vma, vma->vm_start, vma->vm_end);
userfaultfd_reset_ctx(vma);
}
}
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [PATCH v2 7/9] mm/mremap: resolve uffd state when the context is dropped, not during the move
2026-11-16 20:02 ` [PATCH v2 7/9] mm/mremap: resolve uffd state when the context is dropped, not during the move Tal Zussman
@ 2026-11-16 20:41 ` David Hildenbrand (Arm)
0 siblings, 0 replies; 20+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-16 20:41 UTC (permalink / raw)
To: Tal Zussman, Andrew Morton, Mike Rapoport, Peter Xu,
James Houghton, Andrea Arcangeli, Suren Baghdasaryan,
Balbir Singh, Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, Pedro Falcato, Michal Hocko, Michał Mirosław,
Andrei Vagin, Muhammad Usama Anjum, Stephen Rothwell,
Arnd Bergmann, Zi Yan, Matthew Brost, Joshua Hahn, Byungchul Park,
Gregory Price, Ying Huang, Alistair Popple, Baolin Wang,
Nico Pache, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, Kiryl Shutsemau, Muchun Song, Oscar Salvador,
Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel
On 11/16/26 21:02, Tal Zussman wrote:
> Without UFFD_FEATURE_EVENT_REMAP, the new VMA of an mremap() is not
> registered with userfaultfd, so the uffd state of its entries is cleared
> while they are moved. If the move fails halfway, the entries are moved
> back into the old VMA, which is still registered, without their uffd
> state. Writes and accesses to them are then no longer reported.
>
> To fix it, leave the entries alone during the move and clear the uffd
> state of the new VMA in mremap_userfaultfd_prep(), right before dropping
> the context, like unregistering does. This walks the page tables of the
> new VMA a second time.
>
> This is done with userfaultfd_clear_ptes(), which also makes the PTEs
> writable again where that is safe. That removes a spurious write fault
> per protected page on private anonymous memory, but adds spurious write
> faults on shmem and hugetlb, as change_protection() makes writable pages
> that were never protected read-only. UFFDIO_UNREGISTER and
> UFFDIO_WRITEPROTECT already behave this way. This will be fixed in
> change_protection() separately.
>
> The entries no longer need per-entry handling during the move, so drop
> uffd_supports_page_table_move() and clear_uffd_wp_pmd(). Whole page
> tables of these VMAs can be moved again.
>
> Fixes: 0cef0bb836e3 ("mm: clear uffd-wp PTE/PMD state on mremap()")
> Fixes: 763f43865fc4 ("mm: preserve RWP marker across PTE rewrites")
> Reviewed-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> Tested-by: Gregory Price (Meta) <gourry@gourry.net>
> Reviewed-by: Gregory Price (Meta) <gourry@gourry.net>
> Signed-off-by: Tal Zussman <tz2294@columbia.edu>
> ---
VMAs should be write-locked, so nobody should stumble over it and walk page tables.
Rmap walkers can stumble over entries, because only the page table locks protect
us here. With your change, that's now actually consistent while doing the move.
I assume there is now a short period in time where the PTE bits don't match the
VMA flags for an RMAP point of view, essentially in-between clearing uffd state
from the VMA and clearing uffd-wp bits.
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v2 8/9] mm/migrate_device: do not migrate a uffd-protected zero page
2026-11-16 20:02 [PATCH v2 0/9] userfaultfd: assorted fixes (part 1) Tal Zussman
` (6 preceding siblings ...)
2026-11-16 20:02 ` [PATCH v2 7/9] mm/mremap: resolve uffd state when the context is dropped, not during the move Tal Zussman
@ 2026-11-16 20:02 ` Tal Zussman
2026-11-16 20:45 ` David Hildenbrand (Arm)
2026-11-16 20:02 ` [PATCH v2 9/9] mm/userfaultfd: check hugetlb end alignment in UFFDIO_UNREGISTER pre-scan Tal Zussman
8 siblings, 1 reply; 20+ messages in thread
From: Tal Zussman @ 2026-11-16 20:02 UTC (permalink / raw)
To: Andrew Morton, Mike Rapoport, Peter Xu, James Houghton,
Andrea Arcangeli, Suren Baghdasaryan, Balbir Singh,
Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka, Jann Horn,
Pedro Falcato, David Hildenbrand, Michal Hocko,
Michał Mirosław, Andrei Vagin, Muhammad Usama Anjum,
Stephen Rothwell, Arnd Bergmann, Zi Yan, Matthew Brost,
Joshua Hahn, Byungchul Park, Gregory Price, Ying Huang,
Alistair Popple, Baolin Wang, Nico Pache, Ryan Roberts, Dev Jain,
Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau, Muchun Song,
Oscar Salvador, Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel, Thomas Hellström,
Lyude Paul, Danilo Krummrich, Felix Kuehling, Jason Gunthorpe,
Leon Romanovsky, dri-devel, intel-xe, nouveau, amd-gfx,
Tal Zussman
Device migration replaces a PTE that maps the zero page with a new PTE
that is built only from the VMA flags, so its userfaultfd WP or RWP
protection is lost. The same applies to the huge zero PMD. Writes and
accesses to the page are then no longer reported to userfaultfd.
To fix it, do not migrate a uffd-protected zero page. Skip it during
collection, and check again under the page table lock before inserting,
as another thread can protect the page after it was collected.
The alternative would be to carry the uffd state over to the new entry,
but a uffd-protected zero page in a range that a driver migrates should
be rare.
Fixes: 292924b26024 ("userfaultfd: wp: apply _PAGE_UFFD_WP bit")
Fixes: a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: Lyude Paul <lyude@redhat.com>
Cc: Danilo Krummrich <dakr@kernel.org>
Cc: Felix Kuehling <Felix.Kuehling@amd.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Leon Romanovsky <leon@kernel.org>
Cc: dri-devel@lists.freedesktop.org
Cc: intel-xe@lists.freedesktop.org
Cc: nouveau@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Reviewed-by: Alistair Popple <apopple@nvidia.com>
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
mm/migrate_device.c | 23 +++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)
diff --git a/mm/migrate_device.c b/mm/migrate_device.c
index 9e19fadfa65c..140b99f72508 100644
--- a/mm/migrate_device.c
+++ b/mm/migrate_device.c
@@ -159,7 +159,16 @@ static int migrate_vma_collect_huge_pmd(pmd_t *pmdp, unsigned long start,
folio = pmd_folio(*pmdp);
if (is_huge_zero_folio(folio)) {
+ /*
+ * Nothing carries the uffd state of the zero page
+ * over, so skip it if its uffd bit is set.
+ */
+ bool protected = userfaultfd_protected(walk->vma) &&
+ pmd_uffd(*pmdp);
+
spin_unlock(ptl);
+ if (protected)
+ return migrate_vma_collect_skip(start, end, walk);
return migrate_vma_collect_hole(start, end, -1, walk);
}
if (pmd_write(*pmdp))
@@ -337,6 +346,9 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
pfn = pte_pfn(pte);
if (is_zero_pfn(pfn) &&
(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) {
+ /* See migrate_vma_collect_huge_pmd(). */
+ if (userfaultfd_protected(vma) && pte_uffd(pte))
+ goto next;
mpfn = MIGRATE_PFN_MIGRATE;
migrate->cpages++;
goto next;
@@ -871,10 +883,14 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
if (userfaultfd_missing(vma))
goto unlock_abort;
- if (is_huge_zero_pmd(*pmdp))
+ if (is_huge_zero_pmd(*pmdp)) {
+ /* The uffd bit can have been set since the collection. */
+ if (userfaultfd_protected(vma) && pmd_uffd(*pmdp))
+ goto unlock_abort;
flush = true;
- else if (!pmd_none(*pmdp))
+ } else if (!pmd_none(*pmdp)) {
goto unlock_abort;
+ }
add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);
@@ -1074,6 +1090,9 @@ static void migrate_vma_insert_page(struct migrate_vma *migrate,
if (!is_zero_pfn(pfn))
goto unlock_abort;
+ /* The uffd bit can have been set since the collection. */
+ if (userfaultfd_protected(vma) && pte_uffd(orig_pte))
+ goto unlock_abort;
flush = true;
} else if (!pte_none(orig_pte))
goto unlock_abort;
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [PATCH v2 8/9] mm/migrate_device: do not migrate a uffd-protected zero page
2026-11-16 20:02 ` [PATCH v2 8/9] mm/migrate_device: do not migrate a uffd-protected zero page Tal Zussman
@ 2026-11-16 20:45 ` David Hildenbrand (Arm)
0 siblings, 0 replies; 20+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-16 20:45 UTC (permalink / raw)
To: Tal Zussman, Andrew Morton, Mike Rapoport, Peter Xu,
James Houghton, Andrea Arcangeli, Suren Baghdasaryan,
Balbir Singh, Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, Pedro Falcato, Michal Hocko, Michał Mirosław,
Andrei Vagin, Muhammad Usama Anjum, Stephen Rothwell,
Arnd Bergmann, Zi Yan, Matthew Brost, Joshua Hahn, Byungchul Park,
Gregory Price, Ying Huang, Alistair Popple, Baolin Wang,
Nico Pache, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, Kiryl Shutsemau, Muchun Song, Oscar Salvador,
Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel, Thomas Hellström,
Lyude Paul, Danilo Krummrich, Felix Kuehling, Jason Gunthorpe,
Leon Romanovsky, dri-devel, intel-xe, nouveau, amd-gfx
On 11/16/26 21:02, Tal Zussman wrote:
> Device migration replaces a PTE that maps the zero page with a new PTE
> that is built only from the VMA flags, so its userfaultfd WP or RWP
> protection is lost. The same applies to the huge zero PMD. Writes and
> accesses to the page are then no longer reported to userfaultfd.
>
> To fix it, do not migrate a uffd-protected zero page. Skip it during
> collection, and check again under the page table lock before inserting,
> as another thread can protect the page after it was collected.
>
> The alternative would be to carry the uffd state over to the new entry,
> but a uffd-protected zero page in a range that a driver migrates should
> be rare.
>
> Fixes: 292924b26024 ("userfaultfd: wp: apply _PAGE_UFFD_WP bit")
> Fixes: a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
> Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
> Cc: Lyude Paul <lyude@redhat.com>
> Cc: Danilo Krummrich <dakr@kernel.org>
> Cc: Felix Kuehling <Felix.Kuehling@amd.com>
> Cc: Jason Gunthorpe <jgg@ziepe.ca>
> Cc: Leon Romanovsky <leon@kernel.org>
> Cc: dri-devel@lists.freedesktop.org
> Cc: intel-xe@lists.freedesktop.org
> Cc: nouveau@lists.freedesktop.org
> Cc: amd-gfx@lists.freedesktop.org
> Reviewed-by: Alistair Popple <apopple@nvidia.com>
> Signed-off-by: Tal Zussman <tz2294@columbia.edu>
> ---
> mm/migrate_device.c | 23 +++++++++++++++++++++--
> 1 file changed, 21 insertions(+), 2 deletions(-)
>
> diff --git a/mm/migrate_device.c b/mm/migrate_device.c
> index 9e19fadfa65c..140b99f72508 100644
> --- a/mm/migrate_device.c
> +++ b/mm/migrate_device.c
> @@ -159,7 +159,16 @@ static int migrate_vma_collect_huge_pmd(pmd_t *pmdp, unsigned long start,
>
> folio = pmd_folio(*pmdp);
> if (is_huge_zero_folio(folio)) {
> + /*
> + * Nothing carries the uffd state of the zero page
> + * over, so skip it if its uffd bit is set.
> + */
> + bool protected = userfaultfd_protected(walk->vma) &&
> + pmd_uffd(*pmdp);
Now that I read it 3 times in a row below, these needs a helper.
userfaultfd_pte_protected() ? userfaultfd_pte?
That might help you to get rid of the local variable (that should have been
called something that includes uffd and should have been const)
Maybe just add a simple helper for the pmd as well and use it below.
--
Cheers,
David
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v2 9/9] mm/userfaultfd: check hugetlb end alignment in UFFDIO_UNREGISTER pre-scan
2026-11-16 20:02 [PATCH v2 0/9] userfaultfd: assorted fixes (part 1) Tal Zussman
` (7 preceding siblings ...)
2026-11-16 20:02 ` [PATCH v2 8/9] mm/migrate_device: do not migrate a uffd-protected zero page Tal Zussman
@ 2026-11-16 20:02 ` Tal Zussman
2026-11-16 20:56 ` David Hildenbrand (Arm)
8 siblings, 1 reply; 20+ messages in thread
From: Tal Zussman @ 2026-11-16 20:02 UTC (permalink / raw)
To: Andrew Morton, Mike Rapoport, Peter Xu, James Houghton,
Andrea Arcangeli, Suren Baghdasaryan, Balbir Singh,
Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka, Jann Horn,
Pedro Falcato, David Hildenbrand, Michal Hocko,
Michał Mirosław, Andrei Vagin, Muhammad Usama Anjum,
Stephen Rothwell, Arnd Bergmann, Zi Yan, Matthew Brost,
Joshua Hahn, Byungchul Park, Gregory Price, Ying Huang,
Alistair Popple, Baolin Wang, Nico Pache, Ryan Roberts, Dev Jain,
Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau, Muchun Song,
Oscar Salvador, Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel, Tal Zussman
UFFDIO_REGISTER and UFFDIO_UNREGISTER both scan the VMAs of the range
before changing anything. For a hugetlb VMA, userfaultfd_register()
rejects a range whose start or end is not huge page aligned there.
However, userfaultfd_unregister() only checks the start.
An unaligned end only fails later, when vma_modify_flags_uffd() cannot
split the VMA there. At that point, userfaultfd_clear_vma() already
cleared the WP or RWP protection. The ioctl returns -EINVAL and the VMA
stays registered, but accesses to its pages are no longer reported. This
requires a caller passing a misaligned range.
To fix it, check the end of a registered VMA in the pre-scan as well.
The ioctl still returns -EINVAL, but no longer unregisters the VMAs
before the misaligned one first.
Fixes: cab350afcbc9 ("userfaultfd: hugetlbfs: allow registration of ranges containing huge pages")
Fixes: f369b07c8614 ("mm/uffd: reset write protection when unregister with wp-mode")
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
mm/userfaultfd.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index bf42fabb5afd..d8abc24353e1 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -3960,6 +3960,8 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
found = false;
cur = vma;
do {
+ const struct userfaultfd_ctx *vma_ctx = cur->vm_userfaultfd_ctx.ctx;
+
cond_resched();
VM_WARN_ON_ONCE(!!cur->vm_userfaultfd_ctx.ctx ^
@@ -3983,6 +3985,15 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
if (!vma_can_userfault(cur, cur->vm_flags, wp_async))
goto out_unlock;
+ /* See the check in userfaultfd_register(). */
+ if (vma_ctx && vma_is_hugetlb(cur) &&
+ end <= cur->vm_end && end > cur->vm_start) {
+ const unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
+
+ if (!IS_ALIGNED(end, vma_hpagesize))
+ goto out_unlock;
+ }
+
found = true;
} for_each_vma_range(vmi, cur, end);
VM_WARN_ON_ONCE(!found);
--
2.39.5
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [PATCH v2 9/9] mm/userfaultfd: check hugetlb end alignment in UFFDIO_UNREGISTER pre-scan
2026-11-16 20:02 ` [PATCH v2 9/9] mm/userfaultfd: check hugetlb end alignment in UFFDIO_UNREGISTER pre-scan Tal Zussman
@ 2026-11-16 20:56 ` David Hildenbrand (Arm)
0 siblings, 0 replies; 20+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-16 20:56 UTC (permalink / raw)
To: Tal Zussman, Andrew Morton, Mike Rapoport, Peter Xu,
James Houghton, Andrea Arcangeli, Suren Baghdasaryan,
Balbir Singh, Liam R. Howlett, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, Pedro Falcato, Michal Hocko, Michał Mirosław,
Andrei Vagin, Muhammad Usama Anjum, Stephen Rothwell,
Arnd Bergmann, Zi Yan, Matthew Brost, Joshua Hahn, Byungchul Park,
Gregory Price, Ying Huang, Alistair Popple, Baolin Wang,
Nico Pache, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, Kiryl Shutsemau, Muchun Song, Oscar Salvador,
Mikołaj Lenczewski
Cc: linux-mm, linux-kernel, linux-fsdevel
On 11/16/26 21:02, Tal Zussman wrote:
> UFFDIO_REGISTER and UFFDIO_UNREGISTER both scan the VMAs of the range
> before changing anything. For a hugetlb VMA, userfaultfd_register()
> rejects a range whose start or end is not huge page aligned there.
> However, userfaultfd_unregister() only checks the start.
>
> An unaligned end only fails later, when vma_modify_flags_uffd() cannot
> split the VMA there. At that point, userfaultfd_clear_vma() already
> cleared the WP or RWP protection. The ioctl returns -EINVAL and the VMA
> stays registered, but accesses to its pages are no longer reported. This
> requires a caller passing a misaligned range.
>
> To fix it, check the end of a registered VMA in the pre-scan as well.
> The ioctl still returns -EINVAL, but no longer unregisters the VMAs
> before the misaligned one first.
>
> Fixes: cab350afcbc9 ("userfaultfd: hugetlbfs: allow registration of ranges containing huge pages")
> Fixes: f369b07c8614 ("mm/uffd: reset write protection when unregister with wp-mode")
> Signed-off-by: Tal Zussman <tz2294@columbia.edu>
Okay, I assume it's not a stable kernel, so we can fix this in a cleaner way.
> ---
> mm/userfaultfd.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
> index bf42fabb5afd..d8abc24353e1 100644
> --- a/mm/userfaultfd.c
> +++ b/mm/userfaultfd.c
> @@ -3960,6 +3960,8 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
> found = false;
> cur = vma;
> do {
> + const struct userfaultfd_ctx *vma_ctx = cur->vm_userfaultfd_ctx.ctx;
> +
> cond_resched();
>
> VM_WARN_ON_ONCE(!!cur->vm_userfaultfd_ctx.ctx ^
> @@ -3983,6 +3985,15 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
> if (!vma_can_userfault(cur, cur->vm_flags, wp_async))
> goto out_unlock;
>
> + /* See the check in userfaultfd_register(). */
> + if (vma_ctx && vma_is_hugetlb(cur) &&
I'm not sure if we really need the vma_ctx. I'd just fail this consistently if
someone passes hugetlb VMAs, independent of if they are registered or not.
> + end <= cur->vm_end && end > cur->vm_start) {
> + const unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
vma_kernel_pagesize(), yuk. :)
> +
> + if (!IS_ALIGNED(end, vma_hpagesize))
> + goto out_unlock;
> + }
> +
> found = true;
> } for_each_vma_range(vmi, cur, end);
> VM_WARN_ON_ONCE(!found);
Can we clean all this up into something simple like:
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -3669,6 +3669,19 @@ static __always_inline int validate_range(struct
mm_struct *mm,
return validate_unaligned_range(mm, start, len);
}
+static bool vma_uffd_range_aligned(struct vm_area_struct *vma,
+ unsigned long start, unsigned long end)
+{
+ unsigned long size;
+
+ if (!vma_is_hugetlb(vma))
+ return true;
+
+ size = huge_page_size(hstate_vma(vma));
+ return IS_ALIGNED(max(start, vma->vm_start), size) &&
+ IS_ALIGNED(min(end, vma->vm_end), size);
+}
+
static int userfaultfd_register(struct userfaultfd_ctx *ctx,
unsigned long arg)
{
@@ -3747,17 +3760,6 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
if (!vma)
goto out_unlock;
- /*
- * If the first vma contains huge pages, make sure start address
- * is aligned to huge page size.
- */
- if (vma_is_hugetlb(vma)) {
- unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
-
- if (start & (vma_hpagesize - 1))
- goto out_unlock;
- }
-
/*
* Search for not compatible vmas.
*/
@@ -3774,6 +3776,8 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
ret = -EINVAL;
if (!vma_can_userfault(cur, vm_flags, wp_async))
goto out_unlock;
+ if (!vma_uffd_range_aligned(cur, start, end))
+ goto out_unlock;
/*
* RWP uses protnone as an access-tracking marker. PROT_NONE
@@ -3797,20 +3801,6 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
ret = -EPERM;
if (unlikely(!(cur->vm_flags & VM_MAYWRITE)))
goto out_unlock;
-
- /*
- * If this vma contains ending address, and huge pages
- * check alignment.
- */
- if (vma_is_hugetlb(cur) && end <= cur->vm_end &&
- end > cur->vm_start) {
- unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
-
- ret = -EINVAL;
-
- if (end & (vma_hpagesize - 1))
- goto out_unlock;
- }
if ((vm_flags & VM_UFFD_WP) && !(cur->vm_flags & VM_MAYWRITE))
goto out_unlock;
@@ -3920,17 +3910,6 @@ static int userfaultfd_unregister(struct userfaultfd_ctx
*ctx,
if (!vma)
goto out_unlock;
- /*
- * If the first vma contains huge pages, make sure start address
- * is aligned to huge page size.
- */
- if (vma_is_hugetlb(vma)) {
- unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
-
- if (start & (vma_hpagesize - 1))
- goto out_unlock;
- }
-
/*
* Search for not compatible vmas.
*/
@@ -3959,6 +3938,8 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
*/
if (!vma_can_userfault(cur, cur->vm_flags, wp_async))
goto out_unlock;
+ if (!vma_uffd_range_aligned(cur, start, end))
+ goto out_unlock;
found = true;
} for_each_vma_range(vmi, cur, end);
--
Cheers,
David
^ permalink raw reply [flat|nested] 20+ messages in thread