From mboxrd@z Thu Jan 1 00:00:00 1970 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 A9CEA39A7EA for ; Mon, 30 Mar 2026 19:34:01 +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=1774899241; cv=none; b=OSN9ohXSt3YluYOIA6V7cwJa5sNVDMeOwGbIH5LLRaP2o/VUQqaatcSyenNiX02FVWqtgXlk7Z8lANqelNjN+A6h2da7B3E6h6C5E+enmhJGy5Z1Ql6yLaFTC7UnpRMiEJyx9YMSb3wQF7Nx7CNR/UnZiUo64AmA8fXWxy2qN7c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774899241; c=relaxed/simple; bh=vgcD6Pt2Z2JGGp/8CAf1C6zvENXppu0BkdjVFGMmpwU=; h=Date:To:From:Subject:Message-Id; b=Fe4k0BPKfrveP02JpLCsEPO+IVwfyTI+GDTdJ8obxHV0KSCD5h0361gWamfACZIbIrIYOmMs7oKE3Zfaud8u0aPiqijV9GGaynG+p7aR/KQwIfISQUGeVYqsscxPD+qG1a8Qmzwa6cLnNBYLE/pYnHDPffcGFEilXWW+fJ3tVQo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=jdZP0zJO; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="jdZP0zJO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5EA61C4CEF7; Mon, 30 Mar 2026 19:34:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1774899241; bh=vgcD6Pt2Z2JGGp/8CAf1C6zvENXppu0BkdjVFGMmpwU=; h=Date:To:From:Subject:From; b=jdZP0zJOL0nA0i98ftg2GmP1/0wJcnqHZ5mznlPe/jJfjEOaS0wDgpNurexR9tgkA Td7y0FyXqzPikNn03FPJVF4ZaY9YC7wL1MFBnTq5h79oASptmBwQZuOMkerFnpDdA9 X03aHFqHn7YDGWOdsUbWh+gvws08rFk1Fy4MAmUs= Date: Mon, 30 Mar 2026 12:34:00 -0700 To: mm-commits@vger.kernel.org,rppt@kernel.org,akpm@linux-foundation.org From: Andrew Morton Subject: [to-be-updated] userfaultfd-retry-copying-with-locks-dropped-in-mfill_atomic_pte_copy.patch removed from -mm tree Message-Id: <20260330193401.5EA61C4CEF7@smtp.kernel.org> Precedence: bulk X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: The quilt patch titled Subject: userfaultfd: retry copying with locks dropped in mfill_atomic_pte_copy() has been removed from the -mm tree. Its filename was userfaultfd-retry-copying-with-locks-dropped-in-mfill_atomic_pte_copy.patch This patch was dropped because an updated version will be issued ------------------------------------------------------ From: "Mike Rapoport (Microsoft)" Subject: userfaultfd: retry copying with locks dropped in mfill_atomic_pte_copy() Date: Fri, 6 Mar 2026 19:18:05 +0200 Implementation of UFFDIO_COPY for anonymous memory might fail to copy data from userspace buffer when the destination VMA is locked (either with mm_lock or with per-VMA lock). In that case, mfill_atomic() releases the locks, retries copying the data with locks dropped and then re-locks the destination VMA and re-establishes PMD. Since this retry-reget dance is only relevant for UFFDIO_COPY and it never happens for other UFFDIO_ operations, make it a part of mfill_atomic_pte_copy() that actually implements UFFDIO_COPY for anonymous memory. As a temporal safety measure to avoid breaking biscection mfill_atomic_pte_copy() makes sure to never return -ENOENT so that the loop in mfill_atomic() won't retry copiyng outside of mmap_lock. This is removed later when shmem implementation will be updated later and the loop in mfill_atomic() will be adjusted. [avagin@google.com: fix lock leak in mfill_get_vma()] Link: https://lkml.kernel.org/r/20260316173829.1126728-1-avagin@google.com [akpm@linux-foundation.org: update mfill_copy_folio_retry()] Link: https://lkml.kernel.org/r/20260316173829.1126728-1-avagin@google.com Link: https://lkml.kernel.org/r/20260306171815.3160826-6-rppt@kernel.org Signed-off-by: Mike Rapoport (Microsoft) Signed-off-by: Andrei Vagin Cc: Andrea Arcangeli Cc: Axel Rasmussen Cc: Baolin Wang Cc: David Hildenbrand Cc: Hugh Dickins Cc: James Houghton Cc: Liam Howlett Cc: Lorenzo Stoakes (Oracle) Cc: Matthew Wilcox (Oracle) Cc: Michal Hocko Cc: Muchun Song Cc: Nikita Kalyazin Cc: Oscar Salvador Cc: Paolo Bonzini Cc: Peter Xu Cc: Sean Christopherson Cc: Shuah Khan Cc: Suren Baghdasaryan Cc: Vlastimil Babka Signed-off-by: Andrew Morton --- mm/userfaultfd.c | 87 ++++++++++++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 29 deletions(-) --- a/mm/userfaultfd.c~userfaultfd-retry-copying-with-locks-dropped-in-mfill_atomic_pte_copy +++ a/mm/userfaultfd.c @@ -159,6 +159,9 @@ static void uffd_mfill_unlock(struct vm_ static void mfill_put_vma(struct mfill_state *state) { + if (!state->vma) + return; + up_read(&state->ctx->map_changing_lock); uffd_mfill_unlock(state->vma); state->vma = NULL; @@ -185,6 +188,7 @@ static int mfill_get_vma(struct mfill_st * request the user to retry later */ down_read(&ctx->map_changing_lock); + state->vma = dst_vma; err = -EAGAIN; if (atomic_read(&ctx->mmap_changing)) goto out_unlock; @@ -207,7 +211,7 @@ static int mfill_get_vma(struct mfill_st goto out_unlock; if (is_vm_hugetlb_page(dst_vma)) - goto out; + return 0; if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma)) goto out_unlock; @@ -215,8 +219,6 @@ static int mfill_get_vma(struct mfill_st uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE)) goto out_unlock; -out: - state->vma = dst_vma; return 0; out_unlock: @@ -403,35 +405,63 @@ static int mfill_copy_folio_locked(struc return ret; } +static int mfill_copy_folio_retry(struct mfill_state *state, struct folio *folio) +{ + unsigned long src_addr = state->src_addr; + void *kaddr; + int err; + + /* retry copying with mm_lock dropped */ + mfill_put_vma(state); + + kaddr = kmap_local_folio(folio, 0); + err = copy_from_user(kaddr, (const void __user *) src_addr, PAGE_SIZE); + kunmap_local(kaddr); + if (unlikely(err)) + return -EFAULT; + + flush_dcache_folio(folio); + + /* reget VMA and PMD, they could change underneath us */ + err = mfill_get_vma(state); + if (err) + return err; + + err = mfill_establish_pmd(state); + if (err) + return err; + + return 0; +} + static int mfill_atomic_pte_copy(struct mfill_state *state) { - struct vm_area_struct *dst_vma = state->vma; unsigned long dst_addr = state->dst_addr; unsigned long src_addr = state->src_addr; uffd_flags_t flags = state->flags; - pmd_t *dst_pmd = state->pmd; struct folio *folio; int ret; - if (!state->folio) { - ret = -ENOMEM; - folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, dst_vma, - dst_addr); - if (!folio) - goto out; - - ret = mfill_copy_folio_locked(folio, src_addr); - - /* fallback to copy_from_user outside mmap_lock */ - if (unlikely(ret)) { - ret = -ENOENT; - state->folio = folio; - /* don't free the page */ - goto out; - } - } else { - folio = state->folio; - state->folio = NULL; + folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, state->vma, dst_addr); + if (!folio) + return -ENOMEM; + + ret = -ENOMEM; + if (mem_cgroup_charge(folio, state->vma->vm_mm, GFP_KERNEL)) + goto out_release; + + ret = mfill_copy_folio_locked(folio, src_addr); + if (unlikely(ret)) { + /* + * Fallback to copy_from_user outside mmap_lock. + * If retry is successful, mfill_copy_folio_locked() returns + * with locks retaken by mfill_get_vma(). + * If there was an error, we must mfill_put_vma() anyway and it + * will take care of unlocking if needed. + */ + ret = mfill_copy_folio_retry(state, folio); + if (ret) + goto out_release; } /* @@ -441,17 +471,16 @@ static int mfill_atomic_pte_copy(struct */ __folio_mark_uptodate(folio); - ret = -ENOMEM; - if (mem_cgroup_charge(folio, dst_vma->vm_mm, GFP_KERNEL)) - goto out_release; - - ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr, + ret = mfill_atomic_install_pte(state->pmd, state->vma, dst_addr, &folio->page, true, flags); if (ret) goto out_release; out: return ret; out_release: + /* Don't return -ENOENT so that our caller won't retry */ + if (ret == -ENOENT) + ret = -EFAULT; folio_put(folio); goto out; } _ Patches currently in -mm which might be from rppt@kernel.org are userfaultfd-move-vma_can_userfault-out-of-line.patch userfaultfd-introduce-vm_uffd_ops.patch shmem-userfaultfd-use-a-vma-callback-to-handle-uffdio_continue.patch userfaultfd-introduce-vm_uffd_ops-alloc_folio.patch shmem-userfaultfd-implement-shmem-uffd-operations-using-vm_uffd_ops.patch userfaultfd-mfill_atomic-remove-retry-logic.patch