Re: [PATCH v3 03/15] userfaultfd: introduce mfill_establish_pmd() helper

From: Harry Yoo (Oracle)

Date: Tue Mar 31 2026 - 03:57:46 EST


On Mon, Mar 30, 2026 at 01:11:04PM +0300, Mike Rapoport wrote:
> From: "Mike Rapoport (Microsoft)" <rppt@xxxxxxxxxx>
>
> There is a lengthy code chunk in mfill_atomic() that establishes the PMD
> for UFFDIO operations. This code may be called twice: first time when the
> copy is performed with VMA/mm locks held and the other time after the copy
> is retried with locks dropped.
>
> Move the code that establishes a PMD into a helper function so it can be
> reused later during refactoring of mfill_atomic_pte_copy().
>
> Signed-off-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
> ---

Looks good to me,
Acked-by: Harry Yoo (Oracle) <harry@xxxxxxxxxx>

> mm/userfaultfd.c | 102 ++++++++++++++++++++++++-----------------------
> 1 file changed, 52 insertions(+), 50 deletions(-)
>
> diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
> index fa9622ec7279..291e5cfed431 100644
> --- a/mm/userfaultfd.c
> +++ b/mm/userfaultfd.c
> +static int mfill_establish_pmd(struct mfill_state *state)
> +{
> + struct mm_struct *dst_mm = state->ctx->mm;
> + pmd_t *dst_pmd, dst_pmdval;
> +
> + dst_pmd = mm_alloc_pmd(dst_mm, state->dst_addr);
> + if (unlikely(!dst_pmd))
> + return -ENOMEM;
> +
> + dst_pmdval = pmdp_get_lockless(dst_pmd);
> + if (unlikely(pmd_none(dst_pmdval)) &&
> + unlikely(__pte_alloc(dst_mm, dst_pmd)))
> + return -ENOMEM;
> +
> + dst_pmdval = pmdp_get_lockless(dst_pmd);
> + /*
> + * If the dst_pmd is THP don't override it and just be strict.
> + * (This includes the case where the PMD used to be THP and
> + * changed back to none after __pte_alloc().)
> + */
> + if (unlikely(!pmd_present(dst_pmdval) || pmd_leaf(dst_pmdval)))

Just noting: ^ this was
pmd_trans_huge() but
now it is pmd_leaf().

but since the present bit is separately checked and hugetlb vma is handled
in a different path I don't expect functional change.

> + return -EEXIST;
> + if (unlikely(pmd_bad(dst_pmdval)))
> + return -EFAULT;
> +
> + state->pmd = dst_pmd;
> + return 0;
> +}

--
Cheers,
Harry / Hyeonggon