From nobody Sat Apr 4 03:19:49 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6144A35BDC2; Thu, 2 Apr 2026 04:12:34 +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=1775103154; cv=none; b=bawP4C/O894nuCJu01IckFFYpmXPe1vDbyfwrhQqlh2t+8m0Kv/dlU9+tzaSqKMTXqChkbTo3x4Goxigh/0BkRwW7bT647J0nNF8/cEBvZMNHfpjn3tXCEWfez57fV+Av8Zsx+rKj0+QINgzb0MiWiMjaV5W7BV1BXOGh6vi8ns= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775103154; c=relaxed/simple; bh=ioV0AIuZ8YXMYtun9GJuzmLOOslMKT49lU11YBPNu3A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I6l0a34HAsa3KvlNRJhhGVJ/3I0xoFgWsKdfhov+VjyVc/DfdClUoy65KVyGYCxOxkwzfSwdWytJ+mIXU+FhihpFl4Ge7x2iNIgl0jsMSXURk6vzbOA5/OFU3XrPPlHcWAfW+z/mqNeq7abMXJHjoj2wCXhwpZdaVlXnP+tKWUs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=O6LpCCvm; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="O6LpCCvm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 990E0C19423; Thu, 2 Apr 2026 04:12:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775103153; bh=ioV0AIuZ8YXMYtun9GJuzmLOOslMKT49lU11YBPNu3A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O6LpCCvmyoASnA3Ul3G2H5VZP5ja5GFeNlfotTaUKFNEDJPBWQasBquvOFTxmwGkX 2Upft2ZfOHlPacRUK7hKJ4YWFsDP8lD9x7bySPvv4lBy5/UG0AiFm4Ol2PM5i2/iJ1 Yv61x1HnIEf/9L68PpHQkRLw0bsKdaXhzqGCBJCoSsfAuex6zu7ZBwJ3lQDiqjWFRE NhIoFNOU1Pv0K5Z6sMOtdiK4AwCqaW+XDKlgPwPV7nI03LG5YADmDduRjqjE3bcAiu Ygw1t0jmuQ3T5xJM1ZQvljWHrzU/5MAjRUmPVAF7L3I/U+C7JjqPjlu88cFRvLFqcv vtCJiSnyz6AJQ== From: Mike Rapoport To: Andrew Morton Cc: Andrea Arcangeli , Andrei Vagin , Axel Rasmussen , Baolin Wang , David Hildenbrand , Harry Yoo , Hugh Dickins , James Houghton , "Liam R. Howlett" , "Lorenzo Stoakes (Oracle)" , "Matthew Wilcox (Oracle)" , Michal Hocko , Mike Rapoport , Muchun Song , Nikita Kalyazin , Oscar Salvador , Paolo Bonzini , Peter Xu , Sean Christopherson , Shuah Khan , Suren Baghdasaryan , Vlastimil Babka , kvm@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 04/15] userfaultfd: introduce mfill_get_vma() and mfill_put_vma() Date: Thu, 2 Apr 2026 07:11:45 +0300 Message-ID: <20260402041156.1377214-5-rppt@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260402041156.1377214-1-rppt@kernel.org> References: <20260402041156.1377214-1-rppt@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Mike Rapoport (Microsoft)" Split the code that finds, locks and verifies VMA from mfill_atomic() into a helper function. This function will be used later during refactoring of mfill_atomic_pte_copy(). Add a counterpart mfill_put_vma() helper that unlocks the VMA and releases map_changing_lock. [avagin@google.com: fix lock leak in mfill_get_vma()] Link: https://lkml.kernel.org/r/20260316173829.1126728-1-avagin@google.com Signed-off-by: Mike Rapoport (Microsoft) Signed-off-by: Andrei Vagin Reviewed-by: Harry Yoo (Oracle) --- mm/userfaultfd.c | 125 ++++++++++++++++++++++++++++------------------- 1 file changed, 75 insertions(+), 50 deletions(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index f653fb3f74df..c6a38db45343 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -157,6 +157,75 @@ static void uffd_mfill_unlock(struct vm_area_struct *v= ma) } #endif =20 +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 =3D NULL; +} + +static int mfill_get_vma(struct mfill_state *state) +{ + struct userfaultfd_ctx *ctx =3D state->ctx; + uffd_flags_t flags =3D state->flags; + struct vm_area_struct *dst_vma; + int err; + + /* + * Make sure the vma is not shared, that the dst range is + * both valid and fully within a single existing vma. + */ + dst_vma =3D uffd_mfill_lock(ctx->mm, state->dst_start, state->len); + if (IS_ERR(dst_vma)) + return PTR_ERR(dst_vma); + + /* + * If memory mappings are changing because of non-cooperative + * operation (e.g. mremap) running in parallel, bail out and + * request the user to retry later + */ + down_read(&ctx->map_changing_lock); + state->vma =3D dst_vma; + err =3D -EAGAIN; + if (atomic_read(&ctx->mmap_changing)) + goto out_unlock; + + err =3D -EINVAL; + + /* + * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but + * it will overwrite vm_ops, so vma_is_anonymous must return false. + */ + if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) && + dst_vma->vm_flags & VM_SHARED)) + goto out_unlock; + + /* + * validate 'mode' now that we know the dst_vma: don't allow + * a wrprotect copy if the userfaultfd didn't register as WP. + */ + if ((flags & MFILL_ATOMIC_WP) && !(dst_vma->vm_flags & VM_UFFD_WP)) + goto out_unlock; + + if (is_vm_hugetlb_page(dst_vma)) + return 0; + + if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma)) + goto out_unlock; + if (!vma_is_shmem(dst_vma) && + uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE)) + goto out_unlock; + + return 0; + +out_unlock: + mfill_put_vma(state); + return err; +} + static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address) { pgd_t *pgd; @@ -767,8 +836,6 @@ static __always_inline ssize_t mfill_atomic(struct user= faultfd_ctx *ctx, .src_addr =3D src_start, .dst_addr =3D dst_start, }; - struct mm_struct *dst_mm =3D ctx->mm; - struct vm_area_struct *dst_vma; long copied =3D 0; ssize_t err; =20 @@ -783,56 +850,17 @@ static __always_inline ssize_t mfill_atomic(struct us= erfaultfd_ctx *ctx, VM_WARN_ON_ONCE(dst_start + len <=3D dst_start); =20 retry: - /* - * Make sure the vma is not shared, that the dst range is - * both valid and fully within a single existing vma. - */ - dst_vma =3D uffd_mfill_lock(dst_mm, dst_start, len); - if (IS_ERR(dst_vma)) { - err =3D PTR_ERR(dst_vma); + err =3D mfill_get_vma(&state); + if (err) goto out; - } - state.vma =3D dst_vma; - - /* - * If memory mappings are changing because of non-cooperative - * operation (e.g. mremap) running in parallel, bail out and - * request the user to retry later - */ - down_read(&ctx->map_changing_lock); - err =3D -EAGAIN; - if (atomic_read(&ctx->mmap_changing)) - goto out_unlock; - - err =3D -EINVAL; - /* - * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but - * it will overwrite vm_ops, so vma_is_anonymous must return false. - */ - if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) && - dst_vma->vm_flags & VM_SHARED)) - goto out_unlock; - - /* - * validate 'mode' now that we know the dst_vma: don't allow - * a wrprotect copy if the userfaultfd didn't register as WP. - */ - if ((flags & MFILL_ATOMIC_WP) && !(dst_vma->vm_flags & VM_UFFD_WP)) - goto out_unlock; =20 /* * If this is a HUGETLB vma, pass off to appropriate routine */ - if (is_vm_hugetlb_page(dst_vma)) - return mfill_atomic_hugetlb(ctx, dst_vma, dst_start, + if (is_vm_hugetlb_page(state.vma)) + return mfill_atomic_hugetlb(ctx, state.vma, dst_start, src_start, len, flags); =20 - if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma)) - goto out_unlock; - if (!vma_is_shmem(dst_vma) && - uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE)) - goto out_unlock; - while (state.src_addr < src_start + len) { VM_WARN_ON_ONCE(state.dst_addr >=3D dst_start + len); =20 @@ -851,8 +879,7 @@ static __always_inline ssize_t mfill_atomic(struct user= faultfd_ctx *ctx, if (unlikely(err =3D=3D -ENOENT)) { void *kaddr; =20 - up_read(&ctx->map_changing_lock); - uffd_mfill_unlock(state.vma); + mfill_put_vma(&state); VM_WARN_ON_ONCE(!state.folio); =20 kaddr =3D kmap_local_folio(state.folio, 0); @@ -881,9 +908,7 @@ static __always_inline ssize_t mfill_atomic(struct user= faultfd_ctx *ctx, break; } =20 -out_unlock: - up_read(&ctx->map_changing_lock); - uffd_mfill_unlock(state.vma); + mfill_put_vma(&state); out: if (state.folio) folio_put(state.folio); --=20 2.53.0