2024-04-04 17:49:17

by Lokesh Gidra

[permalink] [raw]
Subject: [PATCH] userfaultfd: change src_folio after ensuring it's unpinned in UFFDIO_MOVE

Commit d7a08838ab74 ("mm: userfaultfd: fix unexpected change to src_folio
when UFFDIO_MOVE fails") moved the src_folio->{mapping, index} changing
to after clearing the page-table and ensuring that it's not pinned. This
avoids failure of swapout+migration and possibly memory corruption.

However, the commit missed fixing it in the huge-page case.

Fixes: adef440691ba ("userfaultfd: UFFDIO_MOVE uABI")
Signed-off-by: Lokesh Gidra <[email protected]>
---
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 9859aa4f7553..89f58c7603b2 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -2259,9 +2259,6 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm
goto unlock_ptls;
}

- folio_move_anon_rmap(src_folio, dst_vma);
- WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr));
-
src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd);
/* Folio got pinned from under us. Put it back and fail the move. */
if (folio_maybe_dma_pinned(src_folio)) {
@@ -2270,6 +2267,9 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm
goto unlock_ptls;
}

+ folio_move_anon_rmap(src_folio, dst_vma);
+ WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr));
+
_dst_pmd = mk_huge_pmd(&src_folio->page, dst_vma->vm_page_prot);
/* Follow mremap() behavior and treat the entry dirty after the move */
_dst_pmd = pmd_mkwrite(pmd_mkdirty(_dst_pmd), dst_vma);
--
2.44.0.478.gd926399ef9-goog



2024-04-04 20:38:07

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH] userfaultfd: change src_folio after ensuring it's unpinned in UFFDIO_MOVE

On Thu, 4 Apr 2024 13:23:08 -0700 Suren Baghdasaryan <[email protected]> wrote:

> > Agreed, I don't think it is required for ->index. (I also don't spot any
> > corresponding READ_ONCE)
>
> Since this patch just got Ack'ed, I'll wait for Andrew to take it into
> mm-unstable and then will send a fix removing those WRITE_ONCE(). That
> way we won't have merge conflicts,

Yes please, it's an unrelated thing.

2024-04-04 20:41:29

by David Hildenbrand

[permalink] [raw]
Subject: Re: [PATCH] userfaultfd: change src_folio after ensuring it's unpinned in UFFDIO_MOVE

On 04.04.24 19:17, Lokesh Gidra wrote:
> Commit d7a08838ab74 ("mm: userfaultfd: fix unexpected change to src_folio
> when UFFDIO_MOVE fails") moved the src_folio->{mapping, index} changing
> to after clearing the page-table and ensuring that it's not pinned. This
> avoids failure of swapout+migration and possibly memory corruption.
>
> However, the commit missed fixing it in the huge-page case.
>
> Fixes: adef440691ba ("userfaultfd: UFFDIO_MOVE uABI")
> Signed-off-by: Lokesh Gidra <[email protected]>
> ---
> 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 9859aa4f7553..89f58c7603b2 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -2259,9 +2259,6 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm
> goto unlock_ptls;
> }
>
> - folio_move_anon_rmap(src_folio, dst_vma);
> - WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr));
> -
> src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd);
> /* Folio got pinned from under us. Put it back and fail the move. */
> if (folio_maybe_dma_pinned(src_folio)) {
> @@ -2270,6 +2267,9 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm
> goto unlock_ptls;
> }
>
> + folio_move_anon_rmap(src_folio, dst_vma);
> + WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr));
> +
> _dst_pmd = mk_huge_pmd(&src_folio->page, dst_vma->vm_page_prot);
> /* Follow mremap() behavior and treat the entry dirty after the move */
> _dst_pmd = pmd_mkwrite(pmd_mkdirty(_dst_pmd), dst_vma);

Ouch

Acked-by: David Hildenbrand <[email protected]>

--
Cheers,

David / dhildenb